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 ScrollArea's display:table #1447

Merged
merged 4 commits into from
Apr 13, 2023
Merged
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
12 changes: 1 addition & 11 deletions apps/builder/app/builder/shared/collapsible-section.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
SectionTitle,
SectionTitleLabel,
SectionTitleButton,
css,
} from "@webstudio-is/design-system";
import { theme } from "@webstudio-is/design-system";
import type { ComponentProps, ReactNode } from "react";
Expand Down Expand Up @@ -45,8 +44,6 @@ type CollapsibleSectionBaseProps = {
onOpenChange: (value: boolean) => void;
};

const rootStyle = css({ display: "grid" });

export const CollapsibleSectionBase = ({
label,
trigger,
Expand All @@ -55,14 +52,7 @@ export const CollapsibleSectionBase = ({
isOpen,
onOpenChange,
}: CollapsibleSectionBaseProps) => (
<Collapsible.Root
// Grid elements inside `display:table` element doesn't use children size and perfectly fit the parent
// I don't know why, but this fixes our issues with ScrollArea
// @todo probably pull request here to allow change display https://github.com/radix-ui/primitives/blob/main/packages/react/scroll-area/src/ScrollArea.tsx#L183
className={rootStyle()}
open={isOpen}
onOpenChange={onOpenChange}
>
<Collapsible.Root open={isOpen} onOpenChange={onOpenChange}>
<Box css={{ boxShadow: `0px 1px 0 ${theme.colors.panelOutline}` }}>
<Collapsible.Trigger asChild>
{trigger ?? (
Expand Down
49 changes: 34 additions & 15 deletions packages/design-system/src/components/scroll-area.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { forwardRef, type ComponentProps, type Ref } from "react";
import { styled, theme } from "../stitches.config";
import { styled, theme, css, type CSS } from "../stitches.config";
import { Root, Viewport, Scrollbar, Thumb } from "@radix-ui/react-scroll-area";

const ScrollAreaRoot = styled(Root, {
boxSizing: "border-box",
overflow: "hidden",
});

const ScrollAreaViewport = styled(Viewport, {
boxSizing: "border-box",
width: "100%",
height: "100%",
borderRadius: "inherit",
});

const ScrollAreaThumb = styled(Thumb, {
boxSizing: "border-box",
background: theme.colors.foregroundMoreSubtle,
Expand Down Expand Up @@ -50,21 +43,47 @@ const ScrollAreaScrollbar = styled(Scrollbar, {
},
});

type ScrollAreaProps = ComponentProps<typeof ScrollAreaViewport>;
const viewPortStyle = css({
boxSizing: "border-box",
width: "100%",
height: "100%",
borderRadius: "inherit",

variants: {
verticalOnly: {
// https://github.com/radix-ui/primitives/issues/926#issuecomment-1015279283
true: { "& > div[style]": { display: "block !important" } },
},
},
});

type ScrollAreaProps = { css?: CSS; verticalOnly?: boolean } & Pick<
ComponentProps<"div">,
"onScroll" | "children"
>;

export const ScrollArea = forwardRef(
({ children, css, onScroll }: ScrollAreaProps, ref: Ref<HTMLDivElement>) => {
(
{ children, css, onScroll, verticalOnly = true }: ScrollAreaProps,
ref: Ref<HTMLDivElement>
) => {
return (
<ScrollAreaRoot scrollHideDelay={0} css={css}>
<ScrollAreaViewport ref={ref} onScroll={onScroll}>
<Viewport
ref={ref}
className={viewPortStyle({ verticalOnly })}
onScroll={onScroll}
>
{children}
</ScrollAreaViewport>
</Viewport>
<ScrollAreaScrollbar orientation="vertical">
<ScrollAreaThumb />
</ScrollAreaScrollbar>
<ScrollAreaScrollbar orientation="horizontal">
<ScrollAreaThumb />
</ScrollAreaScrollbar>
{verticalOnly === false && (
<ScrollAreaScrollbar orientation="horizontal">
<ScrollAreaThumb />
</ScrollAreaScrollbar>
)}
</ScrollAreaRoot>
);
}
Expand Down