-
Notifications
You must be signed in to change notification settings - Fork 22
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 checkbox component
- Loading branch information
Showing
8 changed files
with
274 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@kadena/react-ui": patch | ||
--- | ||
|
||
Add new checkbox component |
101 changes: 101 additions & 0 deletions
101
packages/libs/react-ui/src/components/Form/Checkbox/Checkbox.css.ts
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,101 @@ | ||
import { style } from '@vanilla-extract/css'; | ||
import { token, uiBaseRegular } from '../../../styles'; | ||
|
||
export const labelClass = style([ | ||
{ | ||
display: 'flex', | ||
color: token('color.text.base.default'), | ||
alignItems: 'flex-start', | ||
lineHeight: token('size.n4'), | ||
cursor: 'pointer', | ||
gap: token('size.n2'), | ||
transition: 'color 0.2s ease', | ||
selectors: { | ||
'&[data-disabled="true"]': { | ||
cursor: 'not-allowed', | ||
color: token('color.text.base.@disabled'), | ||
}, | ||
'&[data-readonly="true"]': { | ||
cursor: 'unset', | ||
}, | ||
}, | ||
}, | ||
uiBaseRegular, | ||
]); | ||
|
||
export const boxClass = style({ | ||
display: 'flex', | ||
flexDirection: 'row', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
borderRadius: token('radius.xs'), | ||
borderStyle: 'solid', | ||
borderWidth: token('border.hairline'), | ||
borderColor: token('color.border.base.bold'), | ||
backgroundColor: token('color.background.input.default'), | ||
transition: 'background-color 0.2s, border-color 0.2s', | ||
width: token('size.n4'), | ||
height: token('size.n4'), | ||
minWidth: token('size.n4'), | ||
minHeight: token('size.n4'), | ||
selectors: { | ||
// hovered | ||
[`${labelClass}[data-hovered="true"] &`]: { | ||
backgroundColor: token('color.background.input.@hover'), | ||
}, | ||
// focused | ||
[`${labelClass}[data-focus-visible="true"] &`]: { | ||
outline: `2px solid ${token('color.border.tint.outline')}`, | ||
outlineOffset: '1px', | ||
backgroundColor: token('color.background.input.@focus'), | ||
}, | ||
// disabled | ||
[`${labelClass}[data-disabled="true"] &`]: { | ||
borderColor: token('color.border.base.@disabled'), | ||
backgroundColor: token('color.background.input.@disabled'), | ||
}, | ||
// selected | ||
'&[data-selected="true"]': { | ||
borderColor: token('color.border.base.boldest'), | ||
backgroundColor: token('color.background.input.inverse.default'), | ||
}, | ||
[`${labelClass}[data-hovered="true"] &[data-selected="true"]`]: { | ||
backgroundColor: token('color.background.input.inverse.@hover'), | ||
}, | ||
[`${labelClass}[data-focus-visible="true"] &[data-selected="true"]`]: { | ||
outline: `2px solid ${token('color.border.tint.outline')}`, | ||
outlineOffset: '1px', | ||
backgroundColor: token('color.background.input.inverse.@focus'), | ||
}, | ||
// readonly | ||
[`${labelClass}[data-readonly="true"] &`]: { | ||
borderColor: token('color.border.base.@disabled'), | ||
}, | ||
[`${labelClass}[data-readonly="true"] &[data-selected="true"]`]: { | ||
backgroundColor: token('color.background.input.@disabled'), | ||
}, | ||
}, | ||
}); | ||
|
||
export const iconClass = style({ | ||
color: token('color.icon.base.inverse.default'), | ||
opacity: 0, | ||
height: token('size.n3'), | ||
width: token('size.n3'), | ||
selectors: { | ||
// selected | ||
[`${boxClass}[data-selected="true"] &`]: { | ||
opacity: 1, | ||
}, | ||
// disabled | ||
[`${labelClass}[data-disabled="true"] ${boxClass}[data-selected="true"] &`]: | ||
{ | ||
color: token('color.icon.base.@disabled'), | ||
}, | ||
// readonly | ||
[`${labelClass}[data-readonly="true"] ${boxClass}[data-selected="true"] &`]: | ||
{ | ||
color: token('color.icon.base.default'), | ||
}, | ||
}, | ||
}); |
108 changes: 108 additions & 0 deletions
108
packages/libs/react-ui/src/components/Form/Checkbox/Checkbox.stories.tsx
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,108 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import React from 'react'; | ||
|
||
import type { ICheckboxProps } from './Checkbox'; | ||
import { Checkbox } from './Checkbox'; | ||
|
||
const meta: Meta<ICheckboxProps> = { | ||
title: 'Form/Checkbox', | ||
parameters: { | ||
status: { type: 'stable' }, | ||
controls: { | ||
hideNoControlsWarning: true, | ||
sort: 'requiredFirst', | ||
}, | ||
docs: { | ||
description: { | ||
component: 'The checkbox component', | ||
}, | ||
}, | ||
}, | ||
argTypes: { | ||
isDisabled: { | ||
control: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
isSelected: { | ||
control: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
isDeterminate: { | ||
control: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
isReadOnly: { | ||
control: { | ||
type: 'boolean', | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
type CheckboxStoryType = StoryObj<ICheckboxProps>; | ||
|
||
export const Base: CheckboxStoryType = { | ||
args: { | ||
children: 'Check this box', | ||
}, | ||
render: (props: ICheckboxProps) => { | ||
return <Checkbox {...props}>{props.children}</Checkbox>; | ||
}, | ||
}; | ||
|
||
export const Determinate: CheckboxStoryType = { | ||
args: { | ||
children: 'Check this box', | ||
isDeterminate: true, | ||
}, | ||
render: (props: ICheckboxProps) => { | ||
return <Checkbox {...props}>{props.children}</Checkbox>; | ||
}, | ||
}; | ||
|
||
export const Disabled: CheckboxStoryType = { | ||
args: { | ||
children: 'Check this box', | ||
isDisabled: true, | ||
}, | ||
render: (props: ICheckboxProps) => { | ||
return <Checkbox {...props}>{props.children}</Checkbox>; | ||
}, | ||
}; | ||
|
||
export const DisabledChecked: CheckboxStoryType = { | ||
args: { | ||
children: 'Check this box', | ||
isDisabled: true, | ||
isSelected: true, | ||
}, | ||
render: (props: ICheckboxProps) => { | ||
return <Checkbox {...props}>{props.children}</Checkbox>; | ||
}, | ||
}; | ||
|
||
export const ReadOnly: CheckboxStoryType = { | ||
args: { | ||
children: 'Check this box', | ||
isReadOnly: true, | ||
}, | ||
render: (props: ICheckboxProps) => { | ||
return <Checkbox {...props}>{props.children}</Checkbox>; | ||
}, | ||
}; | ||
|
||
export const ReadOnlyChecked: CheckboxStoryType = { | ||
args: { | ||
children: 'Check this box', | ||
isReadOnly: true, | ||
isSelected: true, | ||
}, | ||
render: (props: ICheckboxProps) => { | ||
return <Checkbox {...props}>{props.children}</Checkbox>; | ||
}, | ||
}; | ||
|
||
export default meta; |
55 changes: 55 additions & 0 deletions
55
packages/libs/react-ui/src/components/Form/Checkbox/Checkbox.tsx
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,55 @@ | ||
import { MonoCheck, MonoRemove } from '@kadena/react-icons'; | ||
import React, { useRef } from 'react'; | ||
import { | ||
VisuallyHidden, | ||
mergeProps, | ||
useCheckbox, | ||
useFocusRing, | ||
useHover, | ||
} from 'react-aria'; | ||
import { useToggleState } from 'react-stately'; | ||
import { boxClass, iconClass, labelClass } from './Checkbox.css'; | ||
|
||
export interface ICheckboxProps { | ||
children: string; | ||
isDisabled?: boolean; | ||
isSelected?: boolean; | ||
isReadOnly?: boolean; | ||
isDeterminate?: boolean; | ||
onChange?: (isSelected: boolean) => void; | ||
} | ||
|
||
export function Checkbox(props: ICheckboxProps) { | ||
const state = useToggleState(props); | ||
const ref = useRef(null); | ||
const { inputProps, labelProps } = useCheckbox(props, state, ref); | ||
const { isFocusVisible, focusProps } = useFocusRing(); | ||
const { isHovered, hoverProps } = useHover(props); | ||
|
||
const { isDisabled, children, isDeterminate, isReadOnly } = props; | ||
|
||
const hovered = isHovered && !isDisabled && !isReadOnly; | ||
|
||
return ( | ||
<label | ||
{...mergeProps(labelProps, hoverProps, focusProps)} | ||
className={labelClass} | ||
data-hovered={hovered} | ||
data-disabled={isDisabled} | ||
data-focus-visible={isFocusVisible} | ||
data-readonly={isReadOnly} | ||
> | ||
<VisuallyHidden> | ||
<input {...mergeProps(inputProps, focusProps)} ref={ref} /> | ||
</VisuallyHidden> | ||
<span className={boxClass} data-selected={state.isSelected} aria-hidden> | ||
{isDeterminate ? ( | ||
<MonoRemove className={iconClass} /> | ||
) : ( | ||
<MonoCheck className={iconClass} /> | ||
)} | ||
</span> | ||
{children} | ||
</label> | ||
); | ||
} |
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.