@maratus-registry/boolean-control 0.0.0-stage → 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 +11 -0
- package/css-files/BooleanControl.types.ts +103 -0
- package/css-files/getCustomBooleanControl.ts +55 -0
- package/css-files/getNativeBooleanControl.ts +61 -0
- package/css-files/index.ts +14 -0
- package/css-files/useBooleanControl.ts +87 -0
- package/css-modules/BooleanControl.module.css +113 -0
- package/css-modules/BooleanControl.tsx +11 -0
- package/css-modules/BooleanControl.types.ts +103 -0
- package/css-modules/getCustomBooleanControl.ts +55 -0
- package/css-modules/getNativeBooleanControl.ts +61 -0
- package/css-modules/index.ts +14 -0
- package/css-modules/useBooleanControl.ts +84 -0
- package/meta.json +190 -0
- package/package.json +19 -4
- package/tailwind-css/BooleanControl.css +117 -0
- package/tailwind-css/BooleanControl.tsx +11 -0
- package/tailwind-css/BooleanControl.types.ts +103 -0
- package/tailwind-css/getCustomBooleanControl.ts +55 -0
- package/tailwind-css/getNativeBooleanControl.ts +61 -0
- package/tailwind-css/index.ts +14 -0
- package/tailwind-css/useBooleanControl.ts +87 -0
- package/README.md +0 -3
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
BooleanControlDisabledBehavior,
|
|
3
|
+
BooleanControlMode,
|
|
4
|
+
BooleanControlProps,
|
|
5
|
+
BooleanControlRole,
|
|
6
|
+
CustomBooleanControlRenderProps,
|
|
7
|
+
NativeBooleanControlRenderProps,
|
|
8
|
+
UseBooleanControlOptions,
|
|
9
|
+
UseBooleanControlResult,
|
|
10
|
+
UseCustomBooleanControlResult,
|
|
11
|
+
UseNativeBooleanControlResult,
|
|
12
|
+
} from './BooleanControl.types'
|
|
13
|
+
export { BooleanControl } from './BooleanControl'
|
|
14
|
+
export { useBooleanControl } from './useBooleanControl'
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
BooleanControlDisabledBehavior,
|
|
3
|
+
BooleanControlDisabledState,
|
|
4
|
+
BooleanControlMode,
|
|
5
|
+
UseBooleanControlOptions,
|
|
6
|
+
UseBooleanControlResult,
|
|
7
|
+
} from './BooleanControl.types'
|
|
8
|
+
import { useFieldContext, useFieldControl } from '@maratus-component/field'
|
|
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.'
|
|
17
|
+
|
|
18
|
+
export function useBooleanControl(
|
|
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
|
|
39
|
+
const field = useFieldContext('BooleanControl')
|
|
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
|
+
})
|
|
51
|
+
const controlProps = {
|
|
52
|
+
...fieldControlProps,
|
|
53
|
+
...validityHandlerProps,
|
|
54
|
+
className: clsx(
|
|
55
|
+
'maratus__BooleanControl__booleanControl',
|
|
56
|
+
fieldControlProps.className,
|
|
57
|
+
),
|
|
58
|
+
ref: mergeRefs(fieldControlProps.ref, validityHandlerProps.ref),
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return mode === 'custom'
|
|
62
|
+
? getCustomBooleanControl({
|
|
63
|
+
checked,
|
|
64
|
+
controlProps,
|
|
65
|
+
disabledState,
|
|
66
|
+
field,
|
|
67
|
+
role,
|
|
68
|
+
})
|
|
69
|
+
: getNativeBooleanControl({
|
|
70
|
+
checked,
|
|
71
|
+
controlProps,
|
|
72
|
+
disabledState,
|
|
73
|
+
field,
|
|
74
|
+
role,
|
|
75
|
+
})
|
|
76
|
+
}
|
|
77
|
+
|
|
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'
|
|
87
|
+
}
|
package/README.md
DELETED