Skip to content

Commit b4461f3

Browse files
committed
feat(context-drawer): implement ContextDrawerButton component
1 parent c685d40 commit b4461f3

File tree

6 files changed

+151
-0
lines changed

6 files changed

+151
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React from 'react';
2+
import {
3+
storybookArgTypes,
4+
storybookExcludedControlParams,
5+
StoryMetaType,
6+
} from '@lg-tools/storybook-utils';
7+
import { StoryObj } from '@storybook/react';
8+
9+
import LeafyGreenProvider from '@leafygreen-ui/leafygreen-provider';
10+
11+
import { ContextDrawerButton } from './ContextDrawerButton';
12+
13+
const meta: StoryMetaType<typeof ContextDrawerButton> = {
14+
title: 'Components/ContextDrawer/ContextDrawerButton',
15+
component: ContextDrawerButton,
16+
parameters: {
17+
default: 'LiveExample',
18+
controls: {
19+
exclude: [...storybookExcludedControlParams],
20+
},
21+
generate: {
22+
combineArgs: {
23+
darkMode: [false, true],
24+
isOpen: [false, true],
25+
},
26+
decorator: (Instance, context) => {
27+
return (
28+
<LeafyGreenProvider darkMode={context?.args.darkMode}>
29+
<Instance />
30+
</LeafyGreenProvider>
31+
);
32+
},
33+
},
34+
},
35+
argTypes: {
36+
darkMode: storybookArgTypes.darkMode,
37+
isOpen: {
38+
control: 'boolean',
39+
},
40+
},
41+
};
42+
export default meta;
43+
44+
export const LiveExample: StoryObj<typeof ContextDrawerButton> = {
45+
render: ({ ...args }) => <ContextDrawerButton {...args} />,
46+
parameters: {
47+
chromatic: {
48+
disableSnapshot: true,
49+
},
50+
},
51+
};
52+
53+
export const Generated: StoryObj<typeof ContextDrawerButton> = {
54+
render: () => <></>,
55+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { css, cx } from '@leafygreen-ui/emotion';
2+
import { Theme } from '@leafygreen-ui/lib';
3+
import { palette } from '@leafygreen-ui/palette';
4+
import { borderRadius, color, spacing } from '@leafygreen-ui/tokens';
5+
6+
const getTriggerStyles = (theme: Theme) => {
7+
const backgroundColor = color[theme].background.info.default;
8+
const textColor = palette.blue[theme === Theme.Dark ? 'light2' : 'dark3'];
9+
10+
return css`
11+
border: none;
12+
background-color: ${backgroundColor};
13+
color: ${textColor};
14+
border-radius: 0 0 ${borderRadius[400]}px ${borderRadius[400]}px;
15+
padding: 0 ${spacing[200]}px;
16+
17+
&:focus-visible {
18+
background-color: ${backgroundColor};
19+
color: ${textColor};
20+
box-shadow: none;
21+
}
22+
23+
&:hover,
24+
&:active {
25+
background-color: ${backgroundColor};
26+
color: ${textColor};
27+
box-shadow: none;
28+
}
29+
`;
30+
};
31+
32+
const getGlyphStyles = (theme: Theme) => css`
33+
& svg[role='presentation'] {
34+
color: ${palette.blue[theme === Theme.Dark ? 'light2' : 'dark2']};
35+
}
36+
`;
37+
38+
export const getButtonStyles = ({
39+
className,
40+
theme,
41+
}: {
42+
className?: string;
43+
theme: Theme;
44+
}) => cx(getTriggerStyles(theme), getGlyphStyles(theme), className);
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import React, { forwardRef } from 'react';
2+
3+
import Button, { Size } from '@leafygreen-ui/button';
4+
import ChevronDown from '@leafygreen-ui/icon/dist/ChevronDown';
5+
import LeafyGreenProvider, {
6+
useDarkMode,
7+
} from '@leafygreen-ui/leafygreen-provider';
8+
9+
import { ButtonCorner, Side } from '../ButtonCorner';
10+
11+
import { getButtonStyles } from './ContextDrawerButton.styles';
12+
import { ContextDrawerButtonProps } from './ContextDrawerButton.types';
13+
14+
export const ContextDrawerButton = forwardRef<
15+
HTMLButtonElement,
16+
ContextDrawerButtonProps
17+
>(({ children, className, isOpen = false, ...rest }, ref) => {
18+
const { darkMode, theme } = useDarkMode();
19+
20+
const label = children ?? (isOpen ? 'Hide' : 'View more');
21+
22+
return (
23+
<LeafyGreenProvider darkMode={darkMode}>
24+
<Button
25+
ref={ref}
26+
rightGlyph={<ChevronDown />}
27+
size={Size.XSmall}
28+
className={getButtonStyles({ className, theme })}
29+
{...rest}
30+
>
31+
<ButtonCorner side={Side.Left} />
32+
{label}
33+
<ButtonCorner side={Side.Right} />
34+
</Button>
35+
</LeafyGreenProvider>
36+
);
37+
});
38+
39+
ContextDrawerButton.displayName = 'ContextDrawerButton';
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { ButtonProps } from '@leafygreen-ui/button';
2+
3+
export interface ContextDrawerButtonProps
4+
extends Pick<ButtonProps, 'children' | 'className' | 'disabled' | 'onClick'> {
5+
/**
6+
* Determines whether the trigger is in the 'open' state.
7+
* @default false
8+
*/
9+
isOpen?: boolean;
10+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { ContextDrawerButton } from './ContextDrawerButton';
2+
export { type ContextDrawerButtonProps } from './ContextDrawerButton.types';

packages/context-drawer/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { ContextDrawerButton, type ContextDrawerButtonProps } from './ContextDrawerButton';

0 commit comments

Comments
 (0)