-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new Toolbar component (#236)
* feat: add initial Toolbar and subcomponent declarations * feat: add initial Toolbar styles structure * feat: add getContrastColor util fn to simplify setting colours * feat: add Toolbar container styles * fix: correct fontSizes of Buttons at various sizes * feat: add ToolbarButton component and styles * feat: add ToolbarGroup component and styles * feat: pass colorScheme in context instead of theme * feat: map ToolbarIcon size depending on size prop passed into Toolbar * feat: add ToolbarIconButton component * feat: update Toolbar stories * fix: add Button stories for sizing
- Loading branch information
Showing
16 changed files
with
395 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Spacer, Text } from '@chakra-ui/react' | ||
import { Meta, StoryFn } from '@storybook/react' | ||
|
||
import { BxsTimeFive, BxUpload } from '~/icons' | ||
|
||
import { Toolbar, ToolbarProps } from './Toolbar' | ||
import { ToolbarButton } from './ToolbarButton' | ||
import { ToolbarDivider } from './ToolbarDivider' | ||
import { ToolbarGroup } from './ToolbarGroup' | ||
import { ToolbarIconButton } from './ToolbarIconButton' | ||
|
||
export default { | ||
title: 'Components/Toolbar', | ||
component: Toolbar, | ||
decorators: [], | ||
tags: ['autodocs'], | ||
} as Meta<ToolbarProps> | ||
|
||
const Template: StoryFn<ToolbarProps> = ({ children, ...args }) => { | ||
return ( | ||
<Toolbar {...args}> | ||
<Text>1 item selected</Text> | ||
<Spacer /> | ||
{children} | ||
<ToolbarGroup> | ||
<ToolbarButton leftIcon={<BxUpload fontSize="1.25rem" />}> | ||
Download | ||
</ToolbarButton> | ||
<ToolbarButton leftIcon={<BxsTimeFive fontSize="1.25rem" />}> | ||
Move | ||
</ToolbarButton> | ||
<ToolbarDivider /> | ||
<ToolbarIconButton icon={<BxUpload />} aria-label="Upload" /> | ||
<ToolbarDivider /> | ||
<ToolbarButton>Cancel</ToolbarButton> | ||
</ToolbarGroup> | ||
</Toolbar> | ||
) | ||
} | ||
export const TemplateExample = Template.bind({}) | ||
|
||
export const NeutralColorScheme = Template.bind({}) | ||
NeutralColorScheme.args = { | ||
colorScheme: 'neutral', | ||
} | ||
|
||
export const SizeXs = Template.bind({}) | ||
SizeXs.args = { | ||
size: 'xs', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { PropsWithChildren } from 'react' | ||
import { | ||
createStylesContext, | ||
Flex, | ||
FlexProps, | ||
ThemingProps, | ||
useMultiStyleConfig, | ||
} from '@chakra-ui/react' | ||
|
||
import { ToolbarProvider } from './ToolbarContext' | ||
|
||
const [ToolbarStylesProvider, useToolbarStyles] = createStylesContext('Toolbar') | ||
|
||
export { useToolbarStyles } | ||
|
||
export interface ToolbarProps extends PropsWithChildren, FlexProps { | ||
colorScheme?: 'main' | 'neutral' | 'sub' | ||
size?: ThemingProps<'Toolbar'>['size'] | ||
} | ||
|
||
/** | ||
* Container for the toolbar. | ||
*/ | ||
export const Toolbar = ({ children, ...props }: ToolbarProps): JSX.Element => { | ||
const styles = useMultiStyleConfig('Toolbar', props) | ||
return ( | ||
<ToolbarProvider {...props}> | ||
<ToolbarStylesProvider value={styles}> | ||
<Flex __css={styles.container} {...props}> | ||
{children} | ||
</Flex> | ||
</ToolbarStylesProvider> | ||
</ToolbarProvider> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Button, ButtonProps } from '~/Button' | ||
|
||
import { useToolbarButtonProps } from './utils/useToolbarButtonProps' | ||
|
||
export type ToolbarButtonProps = ButtonProps | ||
|
||
export const ToolbarButton = (props: ToolbarButtonProps): JSX.Element => { | ||
const toolbarButtonProps = useToolbarButtonProps() | ||
|
||
return <Button variant="clear" {...toolbarButtonProps} {...props} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { createContext, FC, PropsWithChildren, useContext } from 'react' | ||
import { ThemingProps } from '@chakra-ui/react' | ||
|
||
export interface ToolbarContextProps { | ||
colorScheme?: 'main' | 'neutral' | 'sub' | ||
size?: ThemingProps<'Toolbar'>['size'] | ||
} | ||
|
||
export type ToolbarContextReturn = Required<ToolbarContextProps> | ||
|
||
const ToolbarContext = createContext<ToolbarContextReturn | undefined>( | ||
undefined, | ||
) | ||
|
||
export const ToolbarProvider: FC<PropsWithChildren<ToolbarContextProps>> = ({ | ||
children, | ||
colorScheme = 'sub', | ||
size = 'md', | ||
}) => { | ||
return ( | ||
<ToolbarContext.Provider value={{ colorScheme, size }}> | ||
{children} | ||
</ToolbarContext.Provider> | ||
) | ||
} | ||
|
||
export const useToolbarContext = () => { | ||
const context = useContext(ToolbarContext) | ||
if (!context) { | ||
throw new Error('useToolbar must be used within a ToolbarProvider') | ||
} | ||
return context | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Divider, DividerProps } from '@chakra-ui/react' | ||
|
||
import { useToolbarStyles } from './Toolbar' | ||
|
||
export type ToolbarDividerProps = DividerProps | ||
|
||
export const ToolbarDivider = (props: ToolbarDividerProps): JSX.Element => { | ||
const styles = useToolbarStyles() | ||
|
||
return <Divider orientation="vertical" __css={styles.divider} {...props} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { Wrap, WrapProps } from '@chakra-ui/react' | ||
|
||
import { useToolbarStyles } from './Toolbar' | ||
|
||
export type ToolbarGroupProps = WrapProps | ||
|
||
export const ToolbarGroup = (props: ToolbarGroupProps): JSX.Element => { | ||
const styles = useToolbarStyles() | ||
return ( | ||
<Wrap | ||
shouldWrapChildren | ||
overflow="initial" | ||
flexDir="row" | ||
__css={styles.group} | ||
spacing="0.5rem" | ||
{...props} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { IconButton, IconButtonProps } from '~/IconButton' | ||
|
||
import { useToolbarButtonProps } from './utils/useToolbarButtonProps' | ||
|
||
export type ToolbarIconButtonProps = IconButtonProps | ||
|
||
export const ToolbarIconButton = ( | ||
props: ToolbarIconButtonProps, | ||
): JSX.Element => { | ||
const toolbarButtonProps = useToolbarButtonProps() | ||
|
||
return <IconButton variant="clear" {...toolbarButtonProps} {...props} /> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from './Toolbar' | ||
export * from './ToolbarButton' | ||
export * from './ToolbarDivider' | ||
export * from './ToolbarGroup' | ||
export * from './ToolbarIconButton' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useMemo } from 'react' | ||
import { useBreakpointValue } from '@chakra-ui/react' | ||
|
||
import { ButtonProps } from '~/Button' | ||
import { layerStyles } from '~/theme/layerStyles' | ||
|
||
import { useToolbarContext } from '../ToolbarContext' | ||
|
||
export const useToolbarButtonProps = (): ButtonProps => { | ||
const { colorScheme, size } = useToolbarContext() | ||
|
||
const toolbarBreakpointSize = useBreakpointValue( | ||
typeof size === 'string' ? { base: size } : size, | ||
) | ||
|
||
const toolbarSize = useMemo(() => { | ||
switch (toolbarBreakpointSize) { | ||
case 'xs': | ||
return 'xs' | ||
default: | ||
return 'sm' | ||
} | ||
}, [toolbarBreakpointSize]) | ||
|
||
const toolbarButtonStyleProps: Partial<ButtonProps> = useMemo(() => { | ||
switch (colorScheme) { | ||
case 'main': | ||
case 'sub': | ||
return { | ||
colorScheme: 'inverse', | ||
_focusVisible: layerStyles.focusRing.inverse._focusVisible, | ||
} | ||
default: | ||
return { colorScheme } | ||
} | ||
}, [colorScheme]) | ||
|
||
return { | ||
...toolbarButtonStyleProps, | ||
size: toolbarSize, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.