Skip to content

Commit

Permalink
fix(ui): add dark theme portal container for storybook + use with Select
Browse files Browse the repository at this point in the history
  • Loading branch information
przemyslaw-wlodek committed May 2, 2024
1 parent 8b5f0cd commit 2c8af5b
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 47 deletions.
2 changes: 1 addition & 1 deletion packages/ui/src/design-system/decorators/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export { page } from './page.decorator';
export * as Variants from './variants-table';
export { Section } from './page-section.component';
export { usePortalContainer } from './page.hooks';
export { useDarkThemePortalContainer } from './page.hooks';
export { UIStateTable } from './ui-state-table.component';
export { ColorSchemaTable } from './color-schema-table.component';
12 changes: 9 additions & 3 deletions packages/ui/src/design-system/decorators/page.component.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { PropsWithChildren } from 'react';
import React, { useEffect, useRef } from 'react';

import { ThemeColorScheme, ThemeProvider } from '../../design-tokens';
import {
LocalThemeProvider,
ThemeColorScheme,
ThemeProvider,
} from '../../design-tokens';
import { Box } from '../box';
import { Divider } from '../divider';
import { Grid, Cell } from '../grid';
import { Cell, Grid } from '../grid';
import { Text } from '../text';

import { usePageContext } from './page.context';
Expand Down Expand Up @@ -48,7 +52,9 @@ export const Page = ({
{children}
</Grid>
</div>
<div id="portal-container" ref={container} />
<LocalThemeProvider colorScheme={ThemeColorScheme.Dark}>
<div id="dark-theme-portal-container" ref={container} />
</LocalThemeProvider>
</ThemeProvider>
);
};
2 changes: 1 addition & 1 deletion packages/ui/src/design-system/decorators/page.hooks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { usePageContext } from './page.context';

export const usePortalContainer = (): HTMLElement | undefined =>
export const useDarkThemePortalContainer = (): HTMLElement | undefined =>
usePageContext().portalContainer;
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type SelectRootProps = Pick<
showArrow?: boolean;
value: string | undefined;
variant?: SelectVariant;
portalContainer?: HTMLElement;
};

const isValidSelectRootChild = (
Expand All @@ -45,6 +46,8 @@ const isValidSelectRootChild = (
* @param onChange `onValueChange` See: https://www.radix-ui.com/primitives/docs/components/select#root
* @param defaultOpen The default value for open state of the select.
* @param placeholder See: https://www.radix-ui.com/primitives/docs/components/select#value
* @param portalContainer The HTMLElement which will be passed as `container` to the Radix `<Select.Portal />`.
* See: https://www.radix-ui.com/primitives/docs/components/select#portal
* @param required See: https://www.radix-ui.com/primitives/docs/components/select#root
* @param showArrow Render arrow icon next to the input value, when the Select is closed.
* @param value See: https://www.radix-ui.com/primitives/docs/components/select#root
Expand All @@ -59,6 +62,7 @@ export const Root = ({
onChange,
defaultOpen = false,
placeholder,
portalContainer,
required,
showArrow = false,
value,
Expand All @@ -84,7 +88,7 @@ export const Root = ({
</Select.Icon>
)}
</Select.Trigger>
<Select.Portal>
<Select.Portal container={portalContainer}>
<Select.Content
className={cx.content[variant]}
position={align === 'selected' ? 'item-aligned' : 'popper'}
Expand Down
44 changes: 43 additions & 1 deletion packages/ui/src/design-system/select/select.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import type { Meta } from '@storybook/react';
import capitalize from 'lodash/capitalize';

import { LocalThemeProvider, ThemeColorScheme } from '../../design-tokens';
import { page, Section, Variants } from '../decorators';
import {
page,
Section,
useDarkThemePortalContainer,
Variants,
} from '../decorators';
import { Divider } from '../divider';
import { Cell, Grid } from '../grid';

Expand Down Expand Up @@ -75,6 +80,7 @@ const SelectAlignment = (): JSX.Element => (
);

const SelectVariants = (): JSX.Element => {
const darkThemePortalContainer = useDarkThemePortalContainer();
const renderedOptions = options.map(option => (
<Select.Item
key={option.value}
Expand Down Expand Up @@ -103,6 +109,11 @@ const SelectVariants = (): JSX.Element => {
value={undefined}
onChange={(): void => void 0}
placeholder={placeholder}
portalContainer={
colorScheme === ThemeColorScheme.Dark
? darkThemePortalContainer
: undefined
}
>
{renderedOptions}
</Select.Root>
Expand All @@ -125,6 +136,11 @@ const SelectVariants = (): JSX.Element => {
onChange={(): void => void 0}
placeholder={placeholder}
showArrow
portalContainer={
colorScheme === ThemeColorScheme.Dark
? darkThemePortalContainer
: undefined
}
>
{renderedOptions}
</Select.Root>
Expand All @@ -139,6 +155,7 @@ const SelectVariants = (): JSX.Element => {
};

const SelectRootVariants = (): JSX.Element => {
const darkThemePortalContainer = useDarkThemePortalContainer();
const renderedOptions = options.map(option => (
<Select.Item
key={option.value}
Expand Down Expand Up @@ -174,6 +191,11 @@ const SelectRootVariants = (): JSX.Element => {
onChange={(): void => void 0}
placeholder={placeholder}
showArrow
portalContainer={
colorScheme === ThemeColorScheme.Dark
? darkThemePortalContainer
: undefined
}
>
{renderedOptions}
</Select.Root>
Expand All @@ -186,6 +208,11 @@ const SelectRootVariants = (): JSX.Element => {
onChange={(): void => void 0}
placeholder={placeholder}
showArrow
portalContainer={
colorScheme === ThemeColorScheme.Dark
? darkThemePortalContainer
: undefined
}
>
{renderedOptions}
</Select.Root>
Expand All @@ -197,6 +224,11 @@ const SelectRootVariants = (): JSX.Element => {
onChange={(): void => void 0}
placeholder={placeholder}
showArrow
portalContainer={
colorScheme === ThemeColorScheme.Dark
? darkThemePortalContainer
: undefined
}
>
{renderedOptions}
</Select.Root>
Expand All @@ -209,6 +241,11 @@ const SelectRootVariants = (): JSX.Element => {
onChange={(): void => void 0}
placeholder={placeholder}
showArrow
portalContainer={
colorScheme === ThemeColorScheme.Dark
? darkThemePortalContainer
: undefined
}
>
{renderedOptions}
</Select.Root>
Expand All @@ -221,6 +258,11 @@ const SelectRootVariants = (): JSX.Element => {
onChange={(): void => void 0}
placeholder={placeholder}
showArrow
portalContainer={
colorScheme === ThemeColorScheme.Dark
? darkThemePortalContainer
: undefined
}
>
{renderedOptions}
</Select.Root>
Expand Down
72 changes: 32 additions & 40 deletions packages/ui/src/design-system/side-drawer/side-drawer.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ThemeColorScheme, LocalThemeProvider, sx } from '../../design-tokens';
import { sleep } from '../../test';
import { Box } from '../box';
import * as Buttons from '../buttons';
import { page, Section, Variants, usePortalContainer } from '../decorators';
import { page, Section, Variants } from '../decorators';
import { Divider } from '../divider';
import { Flex } from '../flex';
import { Grid, Cell } from '../grid';
Expand Down Expand Up @@ -403,46 +403,38 @@ type Interactions = ComponentStory<ElementType<Props>>;
export const Interactions: Interactions = ({
onCloseClick,
onBackClick,
}: Props): JSX.Element => {
const container = usePortalContainer();

return (
<Grid columns="$1">
<Cell>
<Section title="Play">
<Root>
<Content
data-testid="side-drawer-content"
zIndex={100}
container={container}
>
<Header
text="Label"
onBackClick={onBackClick}
onCloseClick={onCloseClick}
}: Props): JSX.Element => (
<Grid columns="$1">
<Cell>
<Section title="Play">
<Root>
<Content data-testid="side-drawer-content" zIndex={100}>
<Header
text="Label"
onBackClick={onBackClick}
onCloseClick={onCloseClick}
/>
<Body>
<Headline
title="Section title"
description="Lorem ipsum dolor sit amet quare id faciam."
/>
<Body>
<Headline
title="Section title"
description="Lorem ipsum dolor sit amet quare id faciam."
/>
</Body>
<Footer>
<Buttons.CallToAction label="Label" />
<Close>
<Buttons.Secondary label="Close" />
</Close>
</Footer>
</Content>
<Trigger>
<Buttons.Primary label="Open side drawer" />
</Trigger>
</Root>
</Section>
</Cell>
</Grid>
);
};
</Body>
<Footer>
<Buttons.CallToAction label="Label" />
<Close>
<Buttons.Secondary label="Close" />
</Close>
</Footer>
</Content>
<Trigger>
<Buttons.Primary label="Open side drawer" />
</Trigger>
</Root>
</Section>
</Cell>
</Grid>
);

Interactions.play = async ({ canvasElement }): Promise<void> => {
const canvas = within(canvasElement);
Expand Down

0 comments on commit 2c8af5b

Please sign in to comment.