-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: checkbox * Create calm-paws-search.md
- Loading branch information
1 parent
8eedf29
commit 0d2a10b
Showing
11 changed files
with
794 additions
and
104 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,6 @@ | ||
--- | ||
"@codeui/kit": patch | ||
"@codeui/playground-next": patch | ||
--- | ||
|
||
feat: checkbox |
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 |
---|---|---|
|
@@ -71,8 +71,8 @@ | |
}, | ||
"packageManager": "[email protected]", | ||
"dependencies": { | ||
"@kobalte/core": "^0.9.2", | ||
"@kobalte/utils": "^0.7.1", | ||
"@kobalte/core": "^0.11.0", | ||
"@kobalte/utils": "^0.9.0", | ||
"@kobalte/vanilla-extract": "^0.4.0", | ||
"@motionone/solid": "^10.16.0", | ||
"@radix-ui/colors": "^0.1.8", | ||
|
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,201 @@ | ||
import { createTheme, style, StyleRule } from "@vanilla-extract/css"; | ||
import { recipe } from "@vanilla-extract/recipes"; | ||
import { mapFontSizeValue, mapSizeValue } from "../../foundation/sizes.css"; | ||
import { themeTokens } from "../../foundation/themes.css"; | ||
import { baseFieldTheme, FieldSizes } from "../Field/Field.css"; | ||
import { tokens } from "../../foundation/contract.css"; | ||
import { styleFieldMessage } from "../Field/fieldStyle"; | ||
|
||
export const [checkboxTheme, checkboxVars] = createTheme({ | ||
size: "", | ||
fontSize: "", | ||
borderColor: tokens.formAccentBorder, | ||
background: tokens.brand, | ||
activeHoverBackground: tokens.brandAccentHover, | ||
nonActiveHoverBackground: tokens.formAccent, | ||
color: tokens.foreground, | ||
radius: `calc(${themeTokens.radii.md} * .6)`, | ||
}); | ||
|
||
export const container = style([ | ||
baseFieldTheme, | ||
checkboxTheme, | ||
{ | ||
display: "inline-flex", | ||
alignItems: "flex-start", | ||
}, | ||
]); | ||
|
||
export const input = style({ | ||
border: 0, | ||
clip: "rect(0px, 0px, 0px, 0px)", | ||
clipPath: "inset(50%)", | ||
height: "1px", | ||
margin: "0px -1px -1px 0px", | ||
overflow: "hidden", | ||
padding: "0px", | ||
position: "absolute", | ||
width: "1px", | ||
whiteSpace: "nowrap", | ||
":focus-visible": { | ||
outline: `2px solid ${tokens.brand}`, | ||
outlineOffset: "2px", | ||
borderRadius: checkboxVars.radius, | ||
}, | ||
}); | ||
|
||
const sizesCss = { | ||
xs: "1rem", | ||
sm: "1.25rem", | ||
md: "1.5rem", | ||
lg: "1.75rem", | ||
xl: "2rem", | ||
}; | ||
|
||
export const icon = style({ | ||
width: checkboxVars.fontSize, | ||
height: checkboxVars.fontSize, | ||
}); | ||
|
||
export const control = recipe({ | ||
base: [ | ||
{ | ||
position: "relative", | ||
height: checkboxVars.size, | ||
width: checkboxVars.size, | ||
overflow: "hidden", | ||
borderRadius: checkboxVars.radius, | ||
":focus-visible": { | ||
outline: `2px solid ${checkboxVars.background}`, | ||
}, | ||
}, | ||
{ | ||
selectors: { | ||
"&:hover::before": { | ||
backgroundColor: checkboxVars.nonActiveHoverBackground, | ||
}, | ||
"&:before": { | ||
content: "", | ||
border: `2px solid ${checkboxVars.borderColor}`, | ||
borderRadius: checkboxVars.radius, | ||
position: "absolute", | ||
width: "100%", | ||
height: "100%", | ||
top: 0, | ||
left: 0, | ||
}, | ||
"&:after": { | ||
content: "", | ||
position: "absolute", | ||
opacity: 0, | ||
background: checkboxVars.background, | ||
borderRadius: checkboxVars.radius, | ||
width: "100%", | ||
height: "100%", | ||
top: 0, | ||
left: 0, | ||
transitionProperty: "transform, opacity", | ||
transitionTimingFunction: "ease", | ||
transitionDuration: ".25s", | ||
transform: `scale(0.5)`, | ||
transformOrigin: "center", | ||
}, | ||
"&:hover::after": { | ||
backgroundColor: checkboxVars.activeHoverBackground, | ||
}, | ||
"[data-checked] &:after": { | ||
opacity: 1, | ||
transform: `scale(1)`, | ||
}, | ||
}, | ||
}, | ||
], | ||
variants: { | ||
size: { | ||
[FieldSizes.xs]: { | ||
vars: { | ||
[checkboxVars.size]: mapSizeValue("xs", sizesCss), | ||
[checkboxVars.fontSize]: mapFontSizeValue("xs"), | ||
[checkboxVars.radius]: `calc(${themeTokens.radii.md} * .5)`, | ||
}, | ||
}, | ||
[FieldSizes.sm]: { | ||
vars: { | ||
[checkboxVars.size]: mapSizeValue("sm", sizesCss), | ||
[checkboxVars.fontSize]: mapFontSizeValue("sm"), | ||
}, | ||
}, | ||
[FieldSizes.md]: { | ||
vars: { | ||
[checkboxVars.size]: mapSizeValue("md", sizesCss), | ||
[checkboxVars.fontSize]: mapFontSizeValue("md"), | ||
}, | ||
}, | ||
[FieldSizes.lg]: { | ||
vars: { | ||
[checkboxVars.size]: mapSizeValue("lg", sizesCss), | ||
[checkboxVars.fontSize]: mapFontSizeValue("md"), | ||
[checkboxVars.radius]: `calc(${themeTokens.radii.md} * .7)`, | ||
}, | ||
}, | ||
[FieldSizes.xl]: { | ||
vars: { | ||
[checkboxVars.size]: mapSizeValue("xl", sizesCss), | ||
[checkboxVars.fontSize]: mapFontSizeValue("lg"), | ||
[checkboxVars.radius]: `calc(${themeTokens.radii.md} * .8)`, | ||
}, | ||
}, | ||
}, | ||
}, | ||
defaultVariants: { | ||
size: "md", | ||
}, | ||
}); | ||
|
||
export const indicator = style({ | ||
display: "flex", | ||
position: "relative", | ||
width: "100%", | ||
height: "100%", | ||
alignItems: "center", | ||
justifyContent: "center", | ||
zIndex: themeTokens.zIndex["10"], | ||
opacity: 0, | ||
transitionProperty: "transform, opacity", | ||
transitionTimingFunction: "ease", | ||
transitionDuration: ".25s", | ||
selectors: { | ||
"[data-checked] &": { | ||
opacity: 1, | ||
}, | ||
}, | ||
}); | ||
|
||
export const label = style([ | ||
{ | ||
userSelect: "none", | ||
selectors: { | ||
...Object.entries(sizesCss).reduce((acc, [size, value]) => { | ||
acc[`[data-field-size=${size}] &`] = { | ||
height: value, | ||
lineHeight: value, | ||
}; | ||
return acc; | ||
}, {} as NonNullable<StyleRule["selectors"]>), | ||
}, | ||
}, | ||
styleFieldMessage({ | ||
xs: "xs", | ||
sm: "xs", | ||
md: "xs", | ||
lg: "md", | ||
xl: "md", | ||
}), | ||
]); | ||
|
||
export const content = style({ | ||
display: "flex", | ||
flexDirection: "column", | ||
alignItems: "flex-start", | ||
marginLeft: themeTokens.spacing["2"], | ||
}); |
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,86 @@ | ||
import { Checkbox as KCheckbox } from "@kobalte/core"; | ||
import { JSX, Ref, Show, splitProps } from "solid-js"; | ||
import * as styles from "./CheckBox.css"; | ||
import { BaseFieldProps, createBaseFieldProps } from "../Field/createBaseFieldProps"; | ||
import { | ||
createFieldErrorMessageProps, | ||
FieldWithErrorMessageSupport, | ||
} from "../Field/FieldError/createFieldErrorMessageProps"; | ||
import { SlotProp } from "../../utils/component"; | ||
import { mergeClasses } from "../../utils/css"; | ||
import { CheckIcon } from "../../icons/CheckIcon"; | ||
import { createFieldMessageProps } from "../Field/FieldMessage/createFieldMessageProps"; | ||
|
||
type CheckBoxSlot = "root" | "input" | "label" | "errorLabel" | "control"; | ||
|
||
export type CheckBoxProps = KCheckbox.CheckboxRootOptions & | ||
BaseFieldProps & | ||
FieldWithErrorMessageSupport & | ||
SlotProp<CheckBoxSlot> & { | ||
description?: string; | ||
label?: JSX.Element; | ||
ref?: Ref<HTMLInputElement>; | ||
}; | ||
|
||
export function Checkbox(props: CheckBoxProps) { | ||
const [local, others] = splitProps(props, [ | ||
"description", | ||
"size", | ||
"label", | ||
"theme", | ||
"errorMessage", | ||
"ref", | ||
"slotClasses", | ||
]); | ||
|
||
const errorMessageProps = createFieldErrorMessageProps(props); | ||
const fieldLabelProps = createFieldMessageProps(props); | ||
|
||
const inputClasses = () => mergeClasses(styles.input, local.slotClasses?.input); | ||
|
||
const labelClasses = () => mergeClasses(styles.label, local.slotClasses?.label); | ||
|
||
const controlClasses = () => | ||
mergeClasses( | ||
styles.control({ | ||
size: local.size, | ||
}), | ||
local.slotClasses?.control, | ||
); | ||
|
||
return ( | ||
<KCheckbox.Root | ||
data-cui={"checkbox"} | ||
data-field-size={local.size} | ||
class={mergeClasses(styles.container, local?.slotClasses?.root)} | ||
{...others} | ||
> | ||
<KCheckbox.Input class={inputClasses()} ref={local.ref} /> | ||
<KCheckbox.Control class={controlClasses()}> | ||
<KCheckbox.Indicator forceMount={true} class={styles.indicator}> | ||
<CheckIcon class={styles.icon} /> | ||
</KCheckbox.Indicator> | ||
</KCheckbox.Control> | ||
|
||
<div class={styles.content}> | ||
<Show when={local.label} keyed={false}> | ||
<KCheckbox.Label class={labelClasses()}>{local.label}</KCheckbox.Label> | ||
</Show> | ||
|
||
<Show when={local.description} keyed={false}> | ||
<KCheckbox.Description {...fieldLabelProps}> | ||
{local.description} | ||
</KCheckbox.Description> | ||
</Show> | ||
|
||
<Show when={errorMessageProps.errorMessage} keyed={false}> | ||
<KCheckbox.ErrorMessage | ||
class={mergeClasses(errorMessageProps.class, local.slotClasses?.errorLabel)} | ||
> | ||
{local.errorMessage} | ||
</KCheckbox.ErrorMessage> | ||
</Show> | ||
</div> | ||
</KCheckbox.Root> | ||
); | ||
} |
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
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.