Skip to content

[DOCS] Guide: Using Pixi UI x ScrollBox #622

@dimitrikochnev

Description

@dimitrikochnev

Hi guys,

Could you please provide an example of how to use ScrollBox (https://pixijs.io/ui/ScrollBox.html) with Pixi React?

Currently, I have the following:

const App = () => {
  const containerRef = useRef();

  return (
    <div ref={containerRef} className="h-full w-full">
      <Application
        className="h-full w-full"
        resizeTo={containerRef}
        resolution={window.devicePixelRatio}
        autoStart
        autoDensity
        sharedTicker
        antialias
      >
        <GridRenderer items={...} />
      </Application>
    </div>
  );
};

and

const GridRenderer = ({ items }) => {
  const { app } = useApplication();
  const scrollRef = useRef(null);
  const itemRefs = useRef([]);

  useEffect(() => {
    const box = scrollRef.current;
    box.removeItems();

    const refs = itemRefs.current;
    box.addItems(refs);

    box.resize(true);
  }, [items, app.screen.width, app.screen.height]);

  return (
    <pixiScrollBox
      ref={scrollRef}
      width={app.screen.width}
      height={app.screen.height}
      elementsMargin={1}
    >
      {items.map((props, i) => (
        <GridItem ref={el => { itemRefs.current[i] = el; }} key={i} {...props} />
      ))}
    </pixiScrollBox>
  );
};

So

  1. I have to use useEffect to move items into the scrollable area, which feels a bit hacky and causes bugs, e.g. resizing makes the scroll position get lost, and adding new items resets it as well.

  2. How can I resize the ScrollBox when the Application's dimensions change and preserve the current scroll position?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions