ToggleButton styles
This commit is contained in:
@@ -60,6 +60,10 @@ export type ItemProps = {
|
|||||||
children: ((props: ItemState) => React.ReactNode) | React.ReactNode
|
children: ((props: ItemState) => React.ReactNode) | React.ReactNode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function useItemContext() {
|
||||||
|
return React.useContext(ItemContext)
|
||||||
|
}
|
||||||
|
|
||||||
function Group({
|
function Group({
|
||||||
children,
|
children,
|
||||||
values: providedValues,
|
values: providedValues,
|
||||||
@@ -228,7 +232,7 @@ function Item({
|
|||||||
|
|
||||||
function Label({children}: React.PropsWithChildren<{}>) {
|
function Label({children}: React.PropsWithChildren<{}>) {
|
||||||
const t = useTheme()
|
const t = useTheme()
|
||||||
const {disabled} = React.useContext(ItemContext)
|
const {disabled} = useItemContext()
|
||||||
return (
|
return (
|
||||||
<Text
|
<Text
|
||||||
style={[
|
style={[
|
||||||
@@ -319,8 +323,7 @@ function createSharedToggleStyles({
|
|||||||
|
|
||||||
function Checkbox() {
|
function Checkbox() {
|
||||||
const t = useTheme()
|
const t = useTheme()
|
||||||
const {selected, hovered, focused, disabled, hasError} =
|
const {selected, hovered, focused, disabled, hasError} = useItemContext()
|
||||||
React.useContext(ItemContext)
|
|
||||||
const {baseStyles, baseHoverStyles, indicatorStyles} =
|
const {baseStyles, baseHoverStyles, indicatorStyles} =
|
||||||
createSharedToggleStyles({
|
createSharedToggleStyles({
|
||||||
theme: t,
|
theme: t,
|
||||||
@@ -368,8 +371,7 @@ function Checkbox() {
|
|||||||
|
|
||||||
function Switch() {
|
function Switch() {
|
||||||
const t = useTheme()
|
const t = useTheme()
|
||||||
const {selected, hovered, focused, disabled, hasError} =
|
const {selected, hovered, focused, disabled, hasError} = useItemContext()
|
||||||
React.useContext(ItemContext)
|
|
||||||
const {baseStyles, baseHoverStyles, indicatorStyles} =
|
const {baseStyles, baseHoverStyles, indicatorStyles} =
|
||||||
createSharedToggleStyles({
|
createSharedToggleStyles({
|
||||||
theme: t,
|
theme: t,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import {View, AccessibilityProps} from 'react-native'
|
import {View, AccessibilityProps, TextStyle, ViewStyle} from 'react-native'
|
||||||
|
|
||||||
import {atoms as a, useTheme} from '#/alf'
|
import {atoms as a, useTheme} from '#/alf'
|
||||||
import {Text} from '#/components/Typography'
|
import {Text} from '#/components/Typography'
|
||||||
@@ -7,6 +7,7 @@ import {Text} from '#/components/Typography'
|
|||||||
import Toggle, {
|
import Toggle, {
|
||||||
GroupProps as BaseGroupProps,
|
GroupProps as BaseGroupProps,
|
||||||
ItemProps as BaseItemProps,
|
ItemProps as BaseItemProps,
|
||||||
|
useItemContext,
|
||||||
} from '#/components/forms/Toggle'
|
} from '#/components/forms/Toggle'
|
||||||
|
|
||||||
export type ItemProps = Omit<BaseItemProps, 'style' | 'role' | 'children'> &
|
export type ItemProps = Omit<BaseItemProps, 'style' | 'role' | 'children'> &
|
||||||
@@ -36,10 +37,60 @@ export function Group({children, multiple, ...props}: GroupProps) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function Button({children, ...props}: ItemProps) {
|
export function Button({children, ...props}: ItemProps) {
|
||||||
const t = useTheme()
|
|
||||||
return (
|
return (
|
||||||
<Toggle.Item {...props}>
|
<Toggle.Item {...props}>
|
||||||
{state => (
|
<ButtonInner>{children}</ButtonInner>
|
||||||
|
</Toggle.Item>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
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 (
|
||||||
<View
|
<View
|
||||||
style={[
|
style={[
|
||||||
a.px_lg,
|
a.px_lg,
|
||||||
@@ -48,18 +99,19 @@ export function Button({children, ...props}: ItemProps) {
|
|||||||
t.atoms.border,
|
t.atoms.border,
|
||||||
{
|
{
|
||||||
borderLeftWidth: 1,
|
borderLeftWidth: 1,
|
||||||
marginLeft: -1, // TODO yuck
|
marginLeft: -1,
|
||||||
backgroundColor: state.selected ? t.palette.black : undefined,
|
|
||||||
},
|
},
|
||||||
|
baseStyles,
|
||||||
|
activeStyles,
|
||||||
|
(state.hovered || state.focused || state.pressed) && hoverStyles,
|
||||||
]}>
|
]}>
|
||||||
{typeof children === 'string' ? (
|
{typeof children === 'string' ? (
|
||||||
<Text
|
<Text
|
||||||
style={[
|
style={[
|
||||||
a.text_center,
|
a.text_center,
|
||||||
a.font_bold,
|
a.font_bold,
|
||||||
state.selected
|
t.atoms.text_contrast_500,
|
||||||
? t.atoms.text_inverted
|
textStyles,
|
||||||
: t.atoms.text_contrast_500,
|
|
||||||
]}>
|
]}>
|
||||||
{children}
|
{children}
|
||||||
</Text>
|
</Text>
|
||||||
@@ -67,8 +119,6 @@ export function Button({children, ...props}: ItemProps) {
|
|||||||
children
|
children
|
||||||
)}
|
)}
|
||||||
</View>
|
</View>
|
||||||
)}
|
|
||||||
</Toggle.Item>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ export function Forms() {
|
|||||||
const [toggleGroupAValues, setToggleGroupAValues] = React.useState(['a'])
|
const [toggleGroupAValues, setToggleGroupAValues] = React.useState(['a'])
|
||||||
const [toggleGroupBValues, setToggleGroupBValues] = React.useState(['a', 'b'])
|
const [toggleGroupBValues, setToggleGroupBValues] = React.useState(['a', 'b'])
|
||||||
const [toggleGroupCValues, setToggleGroupCValues] = React.useState(['a', 'b'])
|
const [toggleGroupCValues, setToggleGroupCValues] = React.useState(['a', 'b'])
|
||||||
|
const [toggleGroupDValues, setToggleGroupDValues] = React.useState(['warn'])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View style={[a.gap_4xl, a.align_start]}>
|
<View style={[a.gap_4xl, a.align_start]}>
|
||||||
@@ -240,8 +241,8 @@ export function Forms() {
|
|||||||
|
|
||||||
<ToggleButton.Group
|
<ToggleButton.Group
|
||||||
label="Preferences"
|
label="Preferences"
|
||||||
values={['warn']}
|
values={toggleGroupDValues}
|
||||||
onChange={e => console.log(e)}>
|
onChange={setToggleGroupDValues}>
|
||||||
<ToggleButton.Button name="hide" label="Hide">
|
<ToggleButton.Button name="hide" label="Hide">
|
||||||
Hide
|
Hide
|
||||||
</ToggleButton.Button>
|
</ToggleButton.Button>
|
||||||
|
|||||||
Reference in New Issue
Block a user