@maratus-registry/boolean-control 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/css-files/BooleanControl.css +115 -0
- package/css-files/BooleanControl.tsx +8 -6
- package/css-files/BooleanControl.types.ts +89 -40
- package/css-files/getCustomBooleanControl.ts +55 -0
- package/css-files/getNativeBooleanControl.ts +61 -0
- package/css-files/index.ts +6 -2
- package/css-files/useBooleanControl.ts +69 -40
- package/css-modules/BooleanControl.module.css +113 -0
- package/css-modules/BooleanControl.tsx +8 -6
- package/css-modules/BooleanControl.types.ts +89 -40
- package/css-modules/getCustomBooleanControl.ts +55 -0
- package/css-modules/getNativeBooleanControl.ts +61 -0
- package/css-modules/index.ts +6 -2
- package/css-modules/useBooleanControl.ts +66 -40
- package/meta.json +187 -2
- package/package.json +2 -2
- package/tailwind-css/BooleanControl.css +117 -0
- package/tailwind-css/BooleanControl.tsx +8 -6
- package/tailwind-css/BooleanControl.types.ts +89 -40
- package/tailwind-css/getCustomBooleanControl.ts +55 -0
- package/tailwind-css/getNativeBooleanControl.ts +61 -0
- package/tailwind-css/index.ts +6 -2
- package/tailwind-css/useBooleanControl.ts +69 -40
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
@layer components {
|
|
2
|
+
:root {
|
|
3
|
+
--ara-boolean-control-appearance: auto;
|
|
4
|
+
--ara-boolean-control-bg: var(--ara-color-control-bg);
|
|
5
|
+
--ara-boolean-control-bg--checked: var(--ara-color-control-bg--checked);
|
|
6
|
+
--ara-boolean-control-bg--disabled: var(--ara-color-control-bg--disabled);
|
|
7
|
+
--ara-boolean-control-bg--focus: var(--ara-color-control-bg--focus);
|
|
8
|
+
--ara-boolean-control-bg--hover: var(--ara-color-control-bg--hover);
|
|
9
|
+
--ara-boolean-control-bg--invalid: var(--ara-color-control-bg--invalid);
|
|
10
|
+
--ara-boolean-control-bg--loading: var(--ara-color-control-bg--loading);
|
|
11
|
+
--ara-boolean-control-bg--readonly: var(--ara-color-control-bg--readonly);
|
|
12
|
+
--ara-boolean-control-border-radius: var(--ara-radius-x1);
|
|
13
|
+
--ara-boolean-control-border-width: var(--ara-border-width-x1);
|
|
14
|
+
--ara-boolean-control-detail: var(--ara-color-control-detail);
|
|
15
|
+
--ara-boolean-control-detail--checked: var(
|
|
16
|
+
--ara-color-control-detail--checked
|
|
17
|
+
);
|
|
18
|
+
--ara-boolean-control-detail--disabled: var(
|
|
19
|
+
--ara-color-control-detail--disabled
|
|
20
|
+
);
|
|
21
|
+
--ara-boolean-control-detail--focus: var(--ara-color-control-detail--focus);
|
|
22
|
+
--ara-boolean-control-detail--hover: var(--ara-color-control-detail--hover);
|
|
23
|
+
--ara-boolean-control-detail--invalid: var(--ara-color-feedback-fg);
|
|
24
|
+
--ara-boolean-control-detail--loading: var(
|
|
25
|
+
--ara-color-control-detail--loading
|
|
26
|
+
);
|
|
27
|
+
--ara-boolean-control-detail--readonly: var(
|
|
28
|
+
--ara-color-control-detail--readonly
|
|
29
|
+
);
|
|
30
|
+
--ara-boolean-control-fg: var(--ara-color-control-fg);
|
|
31
|
+
--ara-boolean-control-fg--checked: var(--ara-color-control-fg--checked);
|
|
32
|
+
--ara-boolean-control-fg--disabled: var(--ara-color-control-fg--disabled);
|
|
33
|
+
--ara-boolean-control-fg--focus: var(--ara-color-control-fg--focus);
|
|
34
|
+
--ara-boolean-control-fg--hover: var(--ara-color-control-fg--hover);
|
|
35
|
+
--ara-boolean-control-fg--invalid: var(--ara-color-feedback-fg);
|
|
36
|
+
--ara-boolean-control-fg--loading: var(--ara-color-control-fg--loading);
|
|
37
|
+
--ara-boolean-control-fg--readonly: var(--ara-color-control-fg--readonly);
|
|
38
|
+
--ara-boolean-control-focus: var(--ara-color-control-focus);
|
|
39
|
+
--ara-boolean-control-focus-offset: var(--ara-spacing-x1);
|
|
40
|
+
--ara-boolean-control-focus-width: var(--ara-border-width-x1);
|
|
41
|
+
--ara-boolean-control-shadow: var(--ara-shadow-control);
|
|
42
|
+
--ara-boolean-control-shadow--checked: var(--ara-shadow-control--checked);
|
|
43
|
+
--ara-boolean-control-shadow--disabled: var(--ara-shadow-control--disabled);
|
|
44
|
+
--ara-boolean-control-shadow--focus: var(--ara-shadow-control--focus);
|
|
45
|
+
--ara-boolean-control-shadow--hover: var(--ara-shadow-control--hover);
|
|
46
|
+
--ara-boolean-control-shadow--invalid: var(--ara-shadow-control--invalid);
|
|
47
|
+
--ara-boolean-control-shadow--loading: var(--ara-shadow-control--loading);
|
|
48
|
+
--ara-boolean-control-shadow--readonly: var(--ara-shadow-control--readonly);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
.maratus__BooleanControl__booleanControl {
|
|
52
|
+
accent-color: var(--ara-boolean-control-bg--checked);
|
|
53
|
+
appearance: var(--ara-boolean-control-appearance);
|
|
54
|
+
background-color: var(--ara-boolean-control-bg);
|
|
55
|
+
border-color: var(--ara-boolean-control-detail);
|
|
56
|
+
border-radius: var(--ara-boolean-control-border-radius);
|
|
57
|
+
border-width: var(--ara-boolean-control-border-width);
|
|
58
|
+
box-shadow: var(--ara-boolean-control-shadow);
|
|
59
|
+
color: var(--ara-boolean-control-fg);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
.maratus__BooleanControl__booleanControl:hover {
|
|
63
|
+
background-color: var(--ara-boolean-control-bg--hover);
|
|
64
|
+
border-color: var(--ara-boolean-control-detail--hover);
|
|
65
|
+
box-shadow: var(--ara-boolean-control-shadow--hover);
|
|
66
|
+
color: var(--ara-boolean-control-fg--hover);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
.maratus__BooleanControl__booleanControl:focus-visible {
|
|
70
|
+
background-color: var(--ara-boolean-control-bg--focus);
|
|
71
|
+
border-color: var(--ara-boolean-control-detail--focus);
|
|
72
|
+
box-shadow: var(--ara-boolean-control-shadow--focus);
|
|
73
|
+
color: var(--ara-boolean-control-fg--focus);
|
|
74
|
+
outline-color: var(--ara-boolean-control-focus);
|
|
75
|
+
outline-offset: var(--ara-boolean-control-focus-offset);
|
|
76
|
+
outline-width: var(--ara-boolean-control-focus-width);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
.maratus__BooleanControl__booleanControl:checked,
|
|
80
|
+
.maratus__BooleanControl__booleanControl[aria-checked='true'] {
|
|
81
|
+
background-color: var(--ara-boolean-control-bg--checked);
|
|
82
|
+
border-color: var(--ara-boolean-control-detail--checked);
|
|
83
|
+
box-shadow: var(--ara-boolean-control-shadow--checked);
|
|
84
|
+
color: var(--ara-boolean-control-fg--checked);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
.maratus__BooleanControl__booleanControl:disabled,
|
|
88
|
+
.maratus__BooleanControl__booleanControl[aria-disabled='true'] {
|
|
89
|
+
background-color: var(--ara-boolean-control-bg--disabled);
|
|
90
|
+
border-color: var(--ara-boolean-control-detail--disabled);
|
|
91
|
+
box-shadow: var(--ara-boolean-control-shadow--disabled);
|
|
92
|
+
color: var(--ara-boolean-control-fg--disabled);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
.maratus__BooleanControl__booleanControl[aria-invalid='true'] {
|
|
96
|
+
background-color: var(--ara-boolean-control-bg--invalid);
|
|
97
|
+
border-color: var(--ara-boolean-control-detail--invalid);
|
|
98
|
+
box-shadow: var(--ara-boolean-control-shadow--invalid);
|
|
99
|
+
color: var(--ara-boolean-control-fg--invalid);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
.maratus__BooleanControl__booleanControl[data-loading] {
|
|
103
|
+
background-color: var(--ara-boolean-control-bg--loading);
|
|
104
|
+
border-color: var(--ara-boolean-control-detail--loading);
|
|
105
|
+
box-shadow: var(--ara-boolean-control-shadow--loading);
|
|
106
|
+
color: var(--ara-boolean-control-fg--loading);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
.maratus__BooleanControl__booleanControl[aria-readonly='true'] {
|
|
110
|
+
background-color: var(--ara-boolean-control-bg--readonly);
|
|
111
|
+
border-color: var(--ara-boolean-control-detail--readonly);
|
|
112
|
+
box-shadow: var(--ara-boolean-control-shadow--readonly);
|
|
113
|
+
color: var(--ara-boolean-control-fg--readonly);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
BooleanControlMode,
|
|
3
|
+
BooleanControlProps,
|
|
4
|
+
} from './BooleanControl.types'
|
|
2
5
|
import { useBooleanControl } from './useBooleanControl'
|
|
3
6
|
|
|
4
|
-
export function BooleanControl
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return children(control)
|
|
7
|
+
export function BooleanControl<TMode extends BooleanControlMode = 'native'>(
|
|
8
|
+
props: BooleanControlProps<TMode>,
|
|
9
|
+
) {
|
|
10
|
+
return props.children(useBooleanControl<TMode>(props))
|
|
9
11
|
}
|
|
@@ -1,54 +1,103 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
FieldContextValue,
|
|
2
3
|
FieldControlRenderProps,
|
|
4
|
+
UseFieldControlOptions,
|
|
3
5
|
ValidityHandlerProps,
|
|
4
6
|
} from '@maratus-component/field'
|
|
5
|
-
import type {
|
|
7
|
+
import type { ReactNode } from 'react'
|
|
8
|
+
|
|
9
|
+
export type BooleanControlMode = keyof BooleanControlModes
|
|
6
10
|
|
|
7
11
|
export type BooleanControlRole = 'checkbox' | 'switch'
|
|
8
12
|
|
|
9
|
-
export type
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
export type BooleanControlDisabledBehavior = 'focusable' | 'native'
|
|
14
|
+
|
|
15
|
+
// The native root is the default, so only a custom root names its mode.
|
|
16
|
+
type BooleanControlModes = {
|
|
17
|
+
custom: {
|
|
18
|
+
options: Omit<UseFieldControlOptions, 'aria-checked'> & {
|
|
19
|
+
checked: boolean
|
|
20
|
+
disabled?: boolean
|
|
21
|
+
disabledBehavior?: BooleanControlDisabledBehavior
|
|
22
|
+
mode: 'custom'
|
|
23
|
+
role: BooleanControlRole
|
|
24
|
+
}
|
|
25
|
+
result: UseCustomBooleanControlResult
|
|
26
|
+
}
|
|
27
|
+
native: {
|
|
28
|
+
options: Omit<UseFieldControlOptions<HTMLInputElement>, 'aria-checked'> & {
|
|
29
|
+
checked?: boolean
|
|
30
|
+
disabled?: boolean
|
|
31
|
+
disabledBehavior?: BooleanControlDisabledBehavior
|
|
32
|
+
mode?: 'native'
|
|
33
|
+
role?: BooleanControlRole
|
|
34
|
+
}
|
|
35
|
+
result: UseNativeBooleanControlResult
|
|
18
36
|
}
|
|
37
|
+
}
|
|
19
38
|
|
|
20
|
-
export type
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export type
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
| 'aria-required'
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
39
|
+
export type UseBooleanControlOptions<
|
|
40
|
+
TMode extends BooleanControlMode = 'native',
|
|
41
|
+
> = BooleanControlModes[TMode]['options'] & { mode?: TMode }
|
|
42
|
+
|
|
43
|
+
export type UseBooleanControlResult<
|
|
44
|
+
TMode extends BooleanControlMode = 'native',
|
|
45
|
+
> = BooleanControlModes[TMode]['result']
|
|
46
|
+
|
|
47
|
+
export type BooleanControlRootProps = FieldControlRenderProps &
|
|
48
|
+
ValidityHandlerProps
|
|
49
|
+
|
|
50
|
+
export type NativeBooleanControlRenderProps = Omit<
|
|
51
|
+
BooleanControlRootProps,
|
|
52
|
+
'aria-checked' | 'aria-disabled' | 'aria-required'
|
|
53
|
+
> & {
|
|
54
|
+
'aria-disabled'?: true
|
|
55
|
+
checked?: boolean
|
|
56
|
+
disabled?: true
|
|
57
|
+
name?: string
|
|
58
|
+
required?: true
|
|
59
|
+
role?: 'switch'
|
|
60
|
+
type: 'checkbox'
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export type UseNativeBooleanControlResult = {
|
|
64
|
+
booleanControlProps: NativeBooleanControlRenderProps
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export type CustomBooleanControlRenderProps = BooleanControlRootProps & {
|
|
68
|
+
'aria-checked': boolean
|
|
69
|
+
'aria-labelledby': string
|
|
70
|
+
role: BooleanControlRole
|
|
71
|
+
tabIndex: number
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export type UseCustomBooleanControlResult = {
|
|
75
|
+
booleanControlProps: CustomBooleanControlRenderProps
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type BooleanControlProps<TMode extends BooleanControlMode = 'native'> =
|
|
79
|
+
UseBooleanControlOptions<TMode> & {
|
|
80
|
+
children: (props: UseBooleanControlResult<TMode>) => ReactNode
|
|
46
81
|
}
|
|
47
82
|
|
|
48
|
-
|
|
49
|
-
|
|
83
|
+
// The disabled option and loading, resolved against disabledBehavior.
|
|
84
|
+
export type BooleanControlDisabledState =
|
|
85
|
+
| 'disabled'
|
|
86
|
+
| 'enabled'
|
|
87
|
+
| 'focusableDisabled'
|
|
88
|
+
|
|
89
|
+
type BooleanControlInput = {
|
|
90
|
+
controlProps: BooleanControlRootProps
|
|
91
|
+
disabledState: BooleanControlDisabledState
|
|
92
|
+
field: FieldContextValue
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export type NativeBooleanControlInput = BooleanControlInput & {
|
|
96
|
+
checked?: boolean
|
|
97
|
+
role?: BooleanControlRole
|
|
50
98
|
}
|
|
51
99
|
|
|
52
|
-
export type
|
|
53
|
-
|
|
100
|
+
export type CustomBooleanControlInput = BooleanControlInput & {
|
|
101
|
+
checked: boolean
|
|
102
|
+
role: BooleanControlRole
|
|
54
103
|
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BooleanControlRootProps,
|
|
3
|
+
CustomBooleanControlInput,
|
|
4
|
+
UseCustomBooleanControlResult,
|
|
5
|
+
} from './BooleanControl.types'
|
|
6
|
+
import { suppressActivation } from '@maratus-lib/props'
|
|
7
|
+
|
|
8
|
+
export function getCustomBooleanControl({
|
|
9
|
+
checked,
|
|
10
|
+
controlProps,
|
|
11
|
+
disabledState,
|
|
12
|
+
field,
|
|
13
|
+
role,
|
|
14
|
+
}: CustomBooleanControlInput): UseCustomBooleanControlResult {
|
|
15
|
+
// Readonly blocks the consumer's onClick, which owns the checked state.
|
|
16
|
+
const isActivationBlocked = disabledState !== 'enabled' || field.isReadOnly
|
|
17
|
+
const { onKeyDown, onKeyUp, ...rootProps } = suppressActivation(
|
|
18
|
+
controlProps,
|
|
19
|
+
isActivationBlocked,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
booleanControlProps: {
|
|
24
|
+
...rootProps,
|
|
25
|
+
'aria-checked': checked,
|
|
26
|
+
...(disabledState === 'enabled' ? {} : { 'aria-disabled': true }),
|
|
27
|
+
// A custom root is not labelable, so it names itself from the label.
|
|
28
|
+
'aria-labelledby': field.labelId,
|
|
29
|
+
onKeyDown: preventSpaceScroll(onKeyDown),
|
|
30
|
+
onKeyUp: isActivationBlocked ? onKeyUp : clickOnSpace(onKeyUp),
|
|
31
|
+
role,
|
|
32
|
+
tabIndex:
|
|
33
|
+
controlProps.tabIndex ?? (disabledState === 'disabled' ? -1 : 0),
|
|
34
|
+
},
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
type KeyHandler = NonNullable<BooleanControlRootProps['onKeyDown']>
|
|
39
|
+
|
|
40
|
+
function preventSpaceScroll(onKeyDown: KeyHandler | undefined): KeyHandler {
|
|
41
|
+
return (event) => {
|
|
42
|
+
if (event.key === ' ') event.preventDefault()
|
|
43
|
+
onKeyDown?.(event)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Space activates on keyup, as on a native checkbox.
|
|
48
|
+
function clickOnSpace(onKeyUp: KeyHandler | undefined): KeyHandler {
|
|
49
|
+
return (event) => {
|
|
50
|
+
onKeyUp?.(event)
|
|
51
|
+
if (event.key !== ' ' || event.defaultPrevented) return
|
|
52
|
+
event.preventDefault()
|
|
53
|
+
event.currentTarget.click()
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BooleanControlRootProps,
|
|
3
|
+
NativeBooleanControlInput,
|
|
4
|
+
UseNativeBooleanControlResult,
|
|
5
|
+
} from './BooleanControl.types'
|
|
6
|
+
import { omitProps, suppressActivation } from '@maratus-lib/props'
|
|
7
|
+
|
|
8
|
+
export function getNativeBooleanControl({
|
|
9
|
+
checked,
|
|
10
|
+
controlProps,
|
|
11
|
+
disabledState,
|
|
12
|
+
field,
|
|
13
|
+
role,
|
|
14
|
+
}: NativeBooleanControlInput): UseNativeBooleanControlResult {
|
|
15
|
+
const isFocusableDisabled = disabledState === 'focusableDisabled'
|
|
16
|
+
// A disabled checkbox gets no click, so consumer activation is skipped too.
|
|
17
|
+
const rootProps = suppressActivation(
|
|
18
|
+
// Native attributes carry these states, so their ARIA forms must not.
|
|
19
|
+
omitProps(controlProps, ['aria-checked', 'aria-disabled', 'aria-required']),
|
|
20
|
+
isFocusableDisabled,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
booleanControlProps: {
|
|
25
|
+
...rootProps,
|
|
26
|
+
// Without checked, the browser keeps the state.
|
|
27
|
+
...(checked === undefined ? {} : { checked }),
|
|
28
|
+
...(disabledState === 'disabled' ? { disabled: true } : {}),
|
|
29
|
+
// A focusable disabled checkbox leaves its form, as a disabled one does.
|
|
30
|
+
...(isFocusableDisabled
|
|
31
|
+
? { 'aria-disabled': true }
|
|
32
|
+
: {
|
|
33
|
+
name: field.name,
|
|
34
|
+
...(field.isRequired ? { required: true } : {}),
|
|
35
|
+
}),
|
|
36
|
+
...(field.isReadOnly || isFocusableDisabled
|
|
37
|
+
? preventToggle(rootProps)
|
|
38
|
+
: {}),
|
|
39
|
+
// A native checkbox already has the checkbox role.
|
|
40
|
+
...(role === 'switch' ? { role } : {}),
|
|
41
|
+
type: 'checkbox',
|
|
42
|
+
},
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Cancelling the click restores the checkbox, and React's onChange fires
|
|
47
|
+
// from that click, so it is dropped too.
|
|
48
|
+
function preventToggle({
|
|
49
|
+
onClick,
|
|
50
|
+
}: Pick<BooleanControlRootProps, 'onClick'>): Pick<
|
|
51
|
+
BooleanControlRootProps,
|
|
52
|
+
'onChange' | 'onClick'
|
|
53
|
+
> {
|
|
54
|
+
return {
|
|
55
|
+
onChange: () => {},
|
|
56
|
+
onClick: (event) => {
|
|
57
|
+
onClick?.(event)
|
|
58
|
+
event.preventDefault()
|
|
59
|
+
},
|
|
60
|
+
}
|
|
61
|
+
}
|
package/css-files/index.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
export type {
|
|
2
|
+
BooleanControlDisabledBehavior,
|
|
3
|
+
BooleanControlMode,
|
|
2
4
|
BooleanControlProps,
|
|
3
|
-
BooleanControlRenderArgs,
|
|
4
|
-
BooleanControlRenderProps,
|
|
5
5
|
BooleanControlRole,
|
|
6
|
+
CustomBooleanControlRenderProps,
|
|
7
|
+
NativeBooleanControlRenderProps,
|
|
6
8
|
UseBooleanControlOptions,
|
|
7
9
|
UseBooleanControlResult,
|
|
10
|
+
UseCustomBooleanControlResult,
|
|
11
|
+
UseNativeBooleanControlResult,
|
|
8
12
|
} from './BooleanControl.types'
|
|
9
13
|
export { BooleanControl } from './BooleanControl'
|
|
10
14
|
export { useBooleanControl } from './useBooleanControl'
|
|
@@ -1,58 +1,87 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
2
|
+
BooleanControlDisabledBehavior,
|
|
3
|
+
BooleanControlDisabledState,
|
|
4
|
+
BooleanControlMode,
|
|
3
5
|
UseBooleanControlOptions,
|
|
4
6
|
UseBooleanControlResult,
|
|
5
7
|
} from './BooleanControl.types'
|
|
6
|
-
import type {
|
|
7
|
-
FieldContextValue,
|
|
8
|
-
FieldControlRenderProps,
|
|
9
|
-
ValidityHandlerProps,
|
|
10
|
-
} from '@maratus-component/field'
|
|
11
8
|
import { useFieldContext, useFieldControl } from '@maratus-component/field'
|
|
12
|
-
import { mergeRefs } from '@maratus-lib/props'
|
|
9
|
+
import { mergeRefs, omitPropWithWarning } from '@maratus-lib/props'
|
|
10
|
+
import clsx from 'clsx'
|
|
11
|
+
import { getCustomBooleanControl } from './getCustomBooleanControl'
|
|
12
|
+
import { getNativeBooleanControl } from './getNativeBooleanControl'
|
|
13
|
+
import './BooleanControl.css'
|
|
14
|
+
|
|
15
|
+
const UNSUPPORTED_READ_ONLY_WARNING =
|
|
16
|
+
'BooleanControl: HTML readonly does not apply to checkboxes. Use FieldRoot isReadOnly instead.'
|
|
13
17
|
|
|
14
18
|
export function useBooleanControl(
|
|
15
|
-
options
|
|
16
|
-
): UseBooleanControlResult
|
|
17
|
-
|
|
19
|
+
options?: UseBooleanControlOptions<'native'>,
|
|
20
|
+
): UseBooleanControlResult<'native'>
|
|
21
|
+
export function useBooleanControl<TMode extends BooleanControlMode>(
|
|
22
|
+
options: UseBooleanControlOptions<TMode>,
|
|
23
|
+
): UseBooleanControlResult<TMode>
|
|
24
|
+
// BooleanControl passes its props through, so its render prop is dropped here.
|
|
25
|
+
export function useBooleanControl(
|
|
26
|
+
options: UseBooleanControlOptions<BooleanControlMode> & {
|
|
27
|
+
children?: unknown
|
|
28
|
+
} = {},
|
|
29
|
+
): UseBooleanControlResult<BooleanControlMode> {
|
|
30
|
+
const {
|
|
31
|
+
checked,
|
|
32
|
+
children: _children,
|
|
33
|
+
disabled,
|
|
34
|
+
disabledBehavior = 'native',
|
|
35
|
+
mode,
|
|
36
|
+
role,
|
|
37
|
+
...fieldControlOptions
|
|
38
|
+
} = options
|
|
18
39
|
const field = useFieldContext('BooleanControl')
|
|
19
|
-
const { fieldControlProps, validityHandlerProps } =
|
|
20
|
-
|
|
40
|
+
const { fieldControlProps, validityHandlerProps } = useFieldControl(
|
|
41
|
+
omitPropWithWarning(
|
|
42
|
+
fieldControlOptions,
|
|
43
|
+
'readOnly',
|
|
44
|
+
UNSUPPORTED_READ_ONLY_WARNING,
|
|
45
|
+
),
|
|
46
|
+
)
|
|
47
|
+
const disabledState = getDisabledState({
|
|
48
|
+
disabledBehavior,
|
|
49
|
+
isDisabled: disabled === true || field.isLoading,
|
|
50
|
+
})
|
|
21
51
|
const controlProps = {
|
|
22
52
|
...fieldControlProps,
|
|
23
53
|
...validityHandlerProps,
|
|
54
|
+
className: clsx(
|
|
55
|
+
'maratus__BooleanControl__booleanControl',
|
|
56
|
+
fieldControlProps.className,
|
|
57
|
+
),
|
|
24
58
|
ref: mergeRefs(fieldControlProps.ref, validityHandlerProps.ref),
|
|
25
59
|
}
|
|
26
60
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
61
|
+
return mode === 'custom'
|
|
62
|
+
? getCustomBooleanControl({
|
|
63
|
+
checked,
|
|
64
|
+
controlProps,
|
|
65
|
+
disabledState,
|
|
66
|
+
field,
|
|
32
67
|
role,
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
68
|
+
})
|
|
69
|
+
: getNativeBooleanControl({
|
|
70
|
+
checked,
|
|
71
|
+
controlProps,
|
|
72
|
+
disabledState,
|
|
73
|
+
field,
|
|
74
|
+
role,
|
|
75
|
+
})
|
|
40
76
|
}
|
|
41
77
|
|
|
42
|
-
function
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return {
|
|
52
|
-
...booleanControlProps,
|
|
53
|
-
...(field.isLoading ? { disabled: true } : {}),
|
|
54
|
-
name: field.name,
|
|
55
|
-
...(field.isRequired ? { required: true } : {}),
|
|
56
|
-
type: 'checkbox',
|
|
57
|
-
}
|
|
78
|
+
function getDisabledState({
|
|
79
|
+
disabledBehavior,
|
|
80
|
+
isDisabled,
|
|
81
|
+
}: {
|
|
82
|
+
disabledBehavior: BooleanControlDisabledBehavior
|
|
83
|
+
isDisabled: boolean
|
|
84
|
+
}): BooleanControlDisabledState {
|
|
85
|
+
if (!isDisabled) return 'enabled'
|
|
86
|
+
return disabledBehavior === 'focusable' ? 'focusableDisabled' : 'disabled'
|
|
58
87
|
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
:root {
|
|
2
|
+
--ara-boolean-control-appearance: auto;
|
|
3
|
+
--ara-boolean-control-bg: var(--ara-color-control-bg);
|
|
4
|
+
--ara-boolean-control-bg--checked: var(--ara-color-control-bg--checked);
|
|
5
|
+
--ara-boolean-control-bg--disabled: var(--ara-color-control-bg--disabled);
|
|
6
|
+
--ara-boolean-control-bg--focus: var(--ara-color-control-bg--focus);
|
|
7
|
+
--ara-boolean-control-bg--hover: var(--ara-color-control-bg--hover);
|
|
8
|
+
--ara-boolean-control-bg--invalid: var(--ara-color-control-bg--invalid);
|
|
9
|
+
--ara-boolean-control-bg--loading: var(--ara-color-control-bg--loading);
|
|
10
|
+
--ara-boolean-control-bg--readonly: var(--ara-color-control-bg--readonly);
|
|
11
|
+
--ara-boolean-control-border-radius: var(--ara-radius-x1);
|
|
12
|
+
--ara-boolean-control-border-width: var(--ara-border-width-x1);
|
|
13
|
+
--ara-boolean-control-detail: var(--ara-color-control-detail);
|
|
14
|
+
--ara-boolean-control-detail--checked: var(
|
|
15
|
+
--ara-color-control-detail--checked
|
|
16
|
+
);
|
|
17
|
+
--ara-boolean-control-detail--disabled: var(
|
|
18
|
+
--ara-color-control-detail--disabled
|
|
19
|
+
);
|
|
20
|
+
--ara-boolean-control-detail--focus: var(--ara-color-control-detail--focus);
|
|
21
|
+
--ara-boolean-control-detail--hover: var(--ara-color-control-detail--hover);
|
|
22
|
+
--ara-boolean-control-detail--invalid: var(--ara-color-feedback-fg);
|
|
23
|
+
--ara-boolean-control-detail--loading: var(
|
|
24
|
+
--ara-color-control-detail--loading
|
|
25
|
+
);
|
|
26
|
+
--ara-boolean-control-detail--readonly: var(
|
|
27
|
+
--ara-color-control-detail--readonly
|
|
28
|
+
);
|
|
29
|
+
--ara-boolean-control-fg: var(--ara-color-control-fg);
|
|
30
|
+
--ara-boolean-control-fg--checked: var(--ara-color-control-fg--checked);
|
|
31
|
+
--ara-boolean-control-fg--disabled: var(--ara-color-control-fg--disabled);
|
|
32
|
+
--ara-boolean-control-fg--focus: var(--ara-color-control-fg--focus);
|
|
33
|
+
--ara-boolean-control-fg--hover: var(--ara-color-control-fg--hover);
|
|
34
|
+
--ara-boolean-control-fg--invalid: var(--ara-color-feedback-fg);
|
|
35
|
+
--ara-boolean-control-fg--loading: var(--ara-color-control-fg--loading);
|
|
36
|
+
--ara-boolean-control-fg--readonly: var(--ara-color-control-fg--readonly);
|
|
37
|
+
--ara-boolean-control-focus: var(--ara-color-control-focus);
|
|
38
|
+
--ara-boolean-control-focus-offset: var(--ara-spacing-x1);
|
|
39
|
+
--ara-boolean-control-focus-width: var(--ara-border-width-x1);
|
|
40
|
+
--ara-boolean-control-shadow: var(--ara-shadow-control);
|
|
41
|
+
--ara-boolean-control-shadow--checked: var(--ara-shadow-control--checked);
|
|
42
|
+
--ara-boolean-control-shadow--disabled: var(--ara-shadow-control--disabled);
|
|
43
|
+
--ara-boolean-control-shadow--focus: var(--ara-shadow-control--focus);
|
|
44
|
+
--ara-boolean-control-shadow--hover: var(--ara-shadow-control--hover);
|
|
45
|
+
--ara-boolean-control-shadow--invalid: var(--ara-shadow-control--invalid);
|
|
46
|
+
--ara-boolean-control-shadow--loading: var(--ara-shadow-control--loading);
|
|
47
|
+
--ara-boolean-control-shadow--readonly: var(--ara-shadow-control--readonly);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
.booleanControl {
|
|
51
|
+
accent-color: var(--ara-boolean-control-bg--checked);
|
|
52
|
+
appearance: var(--ara-boolean-control-appearance);
|
|
53
|
+
background-color: var(--ara-boolean-control-bg);
|
|
54
|
+
border-color: var(--ara-boolean-control-detail);
|
|
55
|
+
border-radius: var(--ara-boolean-control-border-radius);
|
|
56
|
+
border-width: var(--ara-boolean-control-border-width);
|
|
57
|
+
box-shadow: var(--ara-boolean-control-shadow);
|
|
58
|
+
color: var(--ara-boolean-control-fg);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
.booleanControl:hover {
|
|
62
|
+
background-color: var(--ara-boolean-control-bg--hover);
|
|
63
|
+
border-color: var(--ara-boolean-control-detail--hover);
|
|
64
|
+
box-shadow: var(--ara-boolean-control-shadow--hover);
|
|
65
|
+
color: var(--ara-boolean-control-fg--hover);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
.booleanControl:focus-visible {
|
|
69
|
+
background-color: var(--ara-boolean-control-bg--focus);
|
|
70
|
+
border-color: var(--ara-boolean-control-detail--focus);
|
|
71
|
+
box-shadow: var(--ara-boolean-control-shadow--focus);
|
|
72
|
+
color: var(--ara-boolean-control-fg--focus);
|
|
73
|
+
outline-color: var(--ara-boolean-control-focus);
|
|
74
|
+
outline-offset: var(--ara-boolean-control-focus-offset);
|
|
75
|
+
outline-width: var(--ara-boolean-control-focus-width);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
.booleanControl:checked,
|
|
79
|
+
.booleanControl[aria-checked='true'] {
|
|
80
|
+
background-color: var(--ara-boolean-control-bg--checked);
|
|
81
|
+
border-color: var(--ara-boolean-control-detail--checked);
|
|
82
|
+
box-shadow: var(--ara-boolean-control-shadow--checked);
|
|
83
|
+
color: var(--ara-boolean-control-fg--checked);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
.booleanControl:disabled,
|
|
87
|
+
.booleanControl[aria-disabled='true'] {
|
|
88
|
+
background-color: var(--ara-boolean-control-bg--disabled);
|
|
89
|
+
border-color: var(--ara-boolean-control-detail--disabled);
|
|
90
|
+
box-shadow: var(--ara-boolean-control-shadow--disabled);
|
|
91
|
+
color: var(--ara-boolean-control-fg--disabled);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
.booleanControl[aria-invalid='true'] {
|
|
95
|
+
background-color: var(--ara-boolean-control-bg--invalid);
|
|
96
|
+
border-color: var(--ara-boolean-control-detail--invalid);
|
|
97
|
+
box-shadow: var(--ara-boolean-control-shadow--invalid);
|
|
98
|
+
color: var(--ara-boolean-control-fg--invalid);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
.booleanControl[data-loading] {
|
|
102
|
+
background-color: var(--ara-boolean-control-bg--loading);
|
|
103
|
+
border-color: var(--ara-boolean-control-detail--loading);
|
|
104
|
+
box-shadow: var(--ara-boolean-control-shadow--loading);
|
|
105
|
+
color: var(--ara-boolean-control-fg--loading);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
.booleanControl[aria-readonly='true'] {
|
|
109
|
+
background-color: var(--ara-boolean-control-bg--readonly);
|
|
110
|
+
border-color: var(--ara-boolean-control-detail--readonly);
|
|
111
|
+
box-shadow: var(--ara-boolean-control-shadow--readonly);
|
|
112
|
+
color: var(--ara-boolean-control-fg--readonly);
|
|
113
|
+
}
|
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
BooleanControlMode,
|
|
3
|
+
BooleanControlProps,
|
|
4
|
+
} from './BooleanControl.types'
|
|
2
5
|
import { useBooleanControl } from './useBooleanControl'
|
|
3
6
|
|
|
4
|
-
export function BooleanControl
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
return children(control)
|
|
7
|
+
export function BooleanControl<TMode extends BooleanControlMode = 'native'>(
|
|
8
|
+
props: BooleanControlProps<TMode>,
|
|
9
|
+
) {
|
|
10
|
+
return props.children(useBooleanControl<TMode>(props))
|
|
9
11
|
}
|