diff --git a/src/components/forms/Toggle.tsx b/src/components/forms/Toggle.tsx index 7f506f862a..ed678e7fd6 100644 --- a/src/components/forms/Toggle.tsx +++ b/src/components/forms/Toggle.tsx @@ -60,6 +60,10 @@ export type ItemProps = { children: ((props: ItemState) => React.ReactNode) | React.ReactNode } +export function useItemContext() { + return React.useContext(ItemContext) +} + function Group({ children, values: providedValues, @@ -228,7 +232,7 @@ function Item({ function Label({children}: React.PropsWithChildren<{}>) { const t = useTheme() - const {disabled} = React.useContext(ItemContext) + const {disabled} = useItemContext() return ( & @@ -36,42 +37,91 @@ export function Group({children, multiple, ...props}: GroupProps) { } export function Button({children, ...props}: ItemProps) { - const t = useTheme() return ( - {state => ( - - {typeof children === 'string' ? ( - - {children} - - ) : ( - children - )} - - )} + {children} ) } +function ButtonInner({children}: React.PropsWithChildren<{}>) { + const t = useTheme() + const state = useItemContext() + + const {baseStyles, hoverStyles, activeStyles, textStyles} = + React.useMemo(() => { + const base: ViewStyle[] = [] + const hover: ViewStyle[] = [] + const active: ViewStyle[] = [] + const text: TextStyle[] = [] + + hover.push(t.atoms.bg_contrast_100) + + if (state.selected) { + active.push({ + backgroundColor: t.palette.contrast_800, + }) + text.push(t.atoms.text_inverted) + hover.push({ + backgroundColor: t.palette.contrast_800, + }) + + if (state.disabled) { + active.push({ + backgroundColor: t.palette.contrast_500, + }) + } + } + + if (state.disabled) { + base.push({ + backgroundColor: t.palette.contrast_100, + }) + text.push({ + opacity: 0.5, + }) + } + + return { + baseStyles: base, + hoverStyles: hover, + activeStyles: active, + textStyles: text, + } + }, [t, state]) + + return ( + + {typeof children === 'string' ? ( + + {children} + + ) : ( + children + )} + + ) +} + export default { Group, Button, diff --git a/src/view/screens/Storybook/Forms.tsx b/src/view/screens/Storybook/Forms.tsx index d02a4bfe7a..c7dd998b6e 100644 --- a/src/view/screens/Storybook/Forms.tsx +++ b/src/view/screens/Storybook/Forms.tsx @@ -15,6 +15,7 @@ export function Forms() { const [toggleGroupAValues, setToggleGroupAValues] = React.useState(['a']) const [toggleGroupBValues, setToggleGroupBValues] = React.useState(['a', 'b']) const [toggleGroupCValues, setToggleGroupCValues] = React.useState(['a', 'b']) + const [toggleGroupDValues, setToggleGroupDValues] = React.useState(['warn']) return ( @@ -240,8 +241,8 @@ export function Forms() { console.log(e)}> + values={toggleGroupDValues} + onChange={setToggleGroupDValues}> Hide