npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@volvo-cars/react-descendants

v0.5.3

Published

A React hook that helps keep track of the indices of descendant components

Downloads

8,919

Readme

React Descendants

Questions? Ask in Slack #vcc-ui

@volvo-cars/react-descendants

A React hook that helps keep track of the indices of descendant components. It's useful for places where the exact rendering order of a descendent component is unknown. It allows each descendant component to know its index relative other components or renders of this specific component.

This way we can use component composition any way we like while knowing the exact index of each component:

<View>
  /*index 0*/
  <AccordionComp>1</AccordionComp>
  /*index 1*/
  <AccordionComp>2</AccordionComp>
  <Block>
    /*index 2*/
    <AccordionComp>3</AccordionComp>
  </Block>
  <Block>
    <Block>
      /*index 3*/
      <AccordionComp>4</AccordionComp>
    </Block>
  </Block>
</View>

Installation

💡 This package includes Typescript definitions

Usage

We first need to wrap the components which we want to know the rendering index for with a DescendantsIndexProvider and consume the index with useDescendantIndex. The hook will return a ref that needs to be passed to the react element to keep track off.

Simple

const { useDescendantIndex, DescendantsIndexProvider } =
  createDescendantsIndexContext();

const Component = () => {
  const { ref, index } = useDescendantIndex();
  return (
    <Block ref={ref}>
      <Text>Index:{index}</Text>
    </Block>
  );
};

const Wrapper = () => {
  return (
    <DescendantsIndexProvider>
      <Component />
      <View>
        <Component />
      </View>
      <Spacer />
      <View>
        <View>
          <Component />
        </View>
      </View>
    </DescendantsIndexProvider>
  );
};
render(<Wrapper />);

Advanced

Example below shows how the component indices update with mounting and unmounting of components. Try clicking on each component.

const { useDescendantIndex, DescendantsIndexProvider } =
  createDescendantsIndexContext();

const Component = ({ onClick, children }) => {
  const { ref, index } = useDescendantIndex();
  return (
    <Block ref={ref} onClick={onClick}>
      <Text>
        Component {children} | index:{index}
      </Text>
    </Block>
  );
};

const Wrapper = () => {
  const [hidden, setHidden] = React.useState([]);
  return (
    <DescendantsIndexProvider>
      {hidden.length ? (
        <Link onClick={() => setHidden([])}>Show all</Link>
      ) : null}
      {hidden.includes(1) ? null : (
        <Component onClick={() => setHidden([...hidden, 1])}>1</Component>
      )}
      <View>
        {hidden.includes(2) ? null : (
          <Component onClick={() => setHidden([...hidden, 2])}>2</Component>
        )}
      </View>
      <Spacer />
      <View>
        <View>
          {hidden.includes(3) ? null : (
            <Component onClick={() => setHidden([...hidden, 3])}>3</Component>
          )}
        </View>
      </View>
      <View>
        <View>
          {hidden.includes(4) ? null : (
            <Component onClick={() => setHidden([...hidden, 4])}>4</Component>
          )}
        </View>
      </View>
      <View>
        <View>
          {hidden.includes(5) ? null : (
            <Component onClick={() => setHidden([...hidden, 5])}>5</Component>
          )}
        </View>
      </View>
    </DescendantsIndexProvider>
  );
};
render(<Wrapper />);

Nested Providers

It's also possible to nest providers however we like, each hook will update its corresponding context value without affecting other contexts.

const { useDescendantIndex, DescendantsIndexProvider } =
  createDescendantsIndexContext();
const {
  useDescendantIndex: useDescendantIndexAccordionComp,
  DescendantsIndexProvider: DescendantsIndexProviderAccordionComp,
} = createDescendantsIndexContext();

const Component = ({ onClick, children }) => {
  const { ref, index } = useDescendantIndex();
  return (
    <Block ref={ref} onClick={onClick}>
      <Text>
        Component {children} | index:{index}
      </Text>
    </Block>
  );
};
const AccordionComp = ({ onClick, children }) => {
  const { ref, index } = useDescendantIndexAccordionComp();
  return (
    <Block ref={ref} onClick={onClick}>
      <Text>
        AccordionComp {children} | index:{index}
      </Text>
    </Block>
  );
};

const Wrapper = () => {
  const [hidden, setHidden] = React.useState([]);
  const [hiddenAccordionComp, setHiddenAccordionComp] = React.useState([]);
  return (
    <DescendantsIndexProvider>
      {hidden.length ? (
        <Link
          onClick={() => {
            setHidden([]);
            setHiddenAccordionComp([]);
          }}
        >
          Show all
        </Link>
      ) : null}
      {hidden.includes(1) ? null : (
        <Component onClick={() => setHidden([...hidden, 1])}>1</Component>
      )}
      <View>
        {hidden.includes(2) ? null : (
          <Component onClick={() => setHidden([...hidden, 2])}>2</Component>
        )}
      </View>
      <Spacer />
      <View>
        <View>
          {hidden.includes(3) ? null : (
            <Component onClick={() => setHidden([...hidden, 3])}>3</Component>
          )}
          <DescendantsIndexProviderAccordionComp>
            {hiddenAccordionComp.includes(1) ? null : (
              <AccordionComp
                onClick={() =>
                  setHiddenAccordionComp([...hiddenAccordionComp, 1])
                }
              >
                1
              </AccordionComp>
            )}
            <View>
              {hiddenAccordionComp.includes(2) ? null : (
                <AccordionComp
                  onClick={() =>
                    setHiddenAccordionComp([...hiddenAccordionComp, 2])
                  }
                >
                  2
                </AccordionComp>
              )}
              <View>
                {hidden.includes(4) ? null : (
                  <Component onClick={() => setHidden([...hidden, 4])}>
                    4
                  </Component>
                )}
              </View>
            </View>
          </DescendantsIndexProviderAccordionComp>
        </View>
      </View>
      <View>
        <View>
          {hidden.includes(5) ? null : (
            <Component onClick={() => setHidden([...hidden, 5])}>5</Component>
          )}
        </View>
      </View>
    </DescendantsIndexProvider>
  );
};
render(<Wrapper />);