Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: scrollshadow chromatic diff issue #1082

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/components/ScrollShadows.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Css } from "src";
import { GridColumn, GridTable, simpleHeader, SimpleHeaderAndData } from "src/components";
import { ScrollShadows } from "src/components/ScrollShadows";
import { zeroTo } from "src/utils/sb";

export default {
component: ScrollShadows,
parameters: { delay: 1_000 },
};

export function Examples() {
Expand All @@ -15,6 +18,15 @@ export function Examples() {
)}
</ScrollShadows>

<div css={Css.mt3.bgWhite.br8.wPx(400).oh.$}>
<ScrollShadows xss={Css.hPx(200).bgWhite.p1.ba.bcGray200.$}>
<GridTable
columns={columns}
rows={[simpleHeader, ...rowData.map((r) => ({ kind: "data" as const, id: r.id, data: r }))]}
/>
</ScrollShadows>
</div>

<div css={Css.mt3.df.fdc.bgWhite.ba.bcGray200.bshBasic.br8.oh.hPx(200).wPx(400).$}>
<div css={Css.pPx(20).pb2.bgWhite.fw6.$}>Fixed Header</div>
<ScrollShadows xss={Css.pxPx(20).fg1.$}>
Expand All @@ -38,3 +50,19 @@ export function Examples() {
</>
);
}

type Row = SimpleHeaderAndData<RowData>;
type RowData = { id: string; address: string; homeowner: string; market: string };

const rowData: RowData[] = zeroTo(10).map((i) => ({
id: `r:${i + 1}`,
address: "1234 Address Lane",
market: "SO Cal",
homeowner: "John Doe",
}));
const columns: GridColumn<Row>[] = [
{ header: "ID", data: (data) => data.id },
{ header: "Homeowner", data: (data) => data.homeowner },
{ header: "Address", data: (data) => data.address },
{ header: "Market", data: (data) => data.market },
];
9 changes: 8 additions & 1 deletion src/components/ScrollShadows.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useResizeObserver } from "@react-aria/utils";
import { ReactNode, useCallback, useMemo, useRef, useState } from "react";
import { ReactNode, useCallback, useLayoutEffect, useMemo, useRef, useState } from "react";
import { Css, Palette, Properties, useTestIds } from "src";

interface ScrollShadowsProps {
Expand Down Expand Up @@ -63,6 +63,13 @@ export function ScrollShadows(props: ScrollShadowsProps) {
const onResize = useCallback(() => scrollRef.current && updateScrollProps(scrollRef.current), []);
useResizeObserver({ ref: scrollRef, onResize });

// Hack for sizing more complicated components that might not be fully drawn on initial paint (such as a GridTable)
useLayoutEffect(() => {
// This setTimeout (without a delay) allows the current execution thread to complete (DOM is painted with the latest render)
// by placing the callback into the async event queue to ensure the fully rendered DOM is used for the resize calculations.
setTimeout(() => onResize(), 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, but why the setTimeout instead of invoking onResize immediately? Maybe worth adding to as an in-source comment? :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I'm not loving the solution - this hack allows us to delay the execution until the current execution thread is completed by placing this callback into the async queue (but without additional delay) since we need the multiple paints to complete before actually trying to size.
A cleaner option perhaps is to just use a normal useEffect which should be called after render, but also not provide any dependencies so it's called after each render since the callback is cheap? 🤔 Doing more testing on that path...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ehhh didn't seem to work on the last chromatic build :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fwiw my has been something in useSetupColumnSizes i.e. this guy:

  // Used to recalculate our columns sizes when columns change
  useEffect(
    () => {
      if (!calculateImmediately.current) {
        const width = resizeRef.current?.clientWidth;
        width && setTableAndColumnWidths(width);
      }
    },
    // TODO: validate this eslint-disable. It was automatically ignored as part of https://app.shortcut.com/homebound-team/story/40033/enable-react-hooks-exhaustive-deps-for-react-projects
    // eslint-disable-next-line react-hooks/exhaustive-deps
    [columns, setTableAndColumnWidths],
  );

I also thought we had a "ruler" div in GridTable somewhere, but I'm not seeing it anymore/atm... 🤔

}, [onResize]);

return (
<div
css={
Expand Down
Loading