@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.
@@ -0,0 +1,117 @@
1
+ @reference "tailwindcss";
2
+
3
+ :root {
4
+ --ara-boolean-control-appearance: auto;
5
+ --ara-boolean-control-bg: var(--ara-color-control-bg);
6
+ --ara-boolean-control-bg--checked: var(--ara-color-control-bg--checked);
7
+ --ara-boolean-control-bg--disabled: var(--ara-color-control-bg--disabled);
8
+ --ara-boolean-control-bg--focus: var(--ara-color-control-bg--focus);
9
+ --ara-boolean-control-bg--hover: var(--ara-color-control-bg--hover);
10
+ --ara-boolean-control-bg--invalid: var(--ara-color-control-bg--invalid);
11
+ --ara-boolean-control-bg--loading: var(--ara-color-control-bg--loading);
12
+ --ara-boolean-control-bg--readonly: var(--ara-color-control-bg--readonly);
13
+ --ara-boolean-control-border-radius: var(--ara-radius-x1);
14
+ --ara-boolean-control-border-width: var(--ara-border-width-x1);
15
+ --ara-boolean-control-detail: var(--ara-color-control-detail);
16
+ --ara-boolean-control-detail--checked: var(
17
+ --ara-color-control-detail--checked
18
+ );
19
+ --ara-boolean-control-detail--disabled: var(
20
+ --ara-color-control-detail--disabled
21
+ );
22
+ --ara-boolean-control-detail--focus: var(--ara-color-control-detail--focus);
23
+ --ara-boolean-control-detail--hover: var(--ara-color-control-detail--hover);
24
+ --ara-boolean-control-detail--invalid: var(--ara-color-feedback-fg);
25
+ --ara-boolean-control-detail--loading: var(
26
+ --ara-color-control-detail--loading
27
+ );
28
+ --ara-boolean-control-detail--readonly: var(
29
+ --ara-color-control-detail--readonly
30
+ );
31
+ --ara-boolean-control-fg: var(--ara-color-control-fg);
32
+ --ara-boolean-control-fg--checked: var(--ara-color-control-fg--checked);
33
+ --ara-boolean-control-fg--disabled: var(--ara-color-control-fg--disabled);
34
+ --ara-boolean-control-fg--focus: var(--ara-color-control-fg--focus);
35
+ --ara-boolean-control-fg--hover: var(--ara-color-control-fg--hover);
36
+ --ara-boolean-control-fg--invalid: var(--ara-color-feedback-fg);
37
+ --ara-boolean-control-fg--loading: var(--ara-color-control-fg--loading);
38
+ --ara-boolean-control-fg--readonly: var(--ara-color-control-fg--readonly);
39
+ --ara-boolean-control-focus: var(--ara-color-control-focus);
40
+ --ara-boolean-control-focus-offset: var(--ara-spacing-x1);
41
+ --ara-boolean-control-focus-width: var(--ara-border-width-x1);
42
+ --ara-boolean-control-shadow: var(--ara-shadow-control);
43
+ --ara-boolean-control-shadow--checked: var(--ara-shadow-control--checked);
44
+ --ara-boolean-control-shadow--disabled: var(--ara-shadow-control--disabled);
45
+ --ara-boolean-control-shadow--focus: var(--ara-shadow-control--focus);
46
+ --ara-boolean-control-shadow--hover: var(--ara-shadow-control--hover);
47
+ --ara-boolean-control-shadow--invalid: var(--ara-shadow-control--invalid);
48
+ --ara-boolean-control-shadow--loading: var(--ara-shadow-control--loading);
49
+ --ara-boolean-control-shadow--readonly: var(--ara-shadow-control--readonly);
50
+ }
51
+
52
+ @layer components {
53
+ .maratus__BooleanControl__booleanControl {
54
+ accent-color: var(--ara-boolean-control-bg--checked);
55
+ appearance: var(--ara-boolean-control-appearance);
56
+ background-color: var(--ara-boolean-control-bg);
57
+ border-color: var(--ara-boolean-control-detail);
58
+ border-radius: var(--ara-boolean-control-border-radius);
59
+ border-width: var(--ara-boolean-control-border-width);
60
+ box-shadow: var(--ara-boolean-control-shadow);
61
+ color: var(--ara-boolean-control-fg);
62
+ }
63
+
64
+ .maratus__BooleanControl__booleanControl:hover {
65
+ background-color: var(--ara-boolean-control-bg--hover);
66
+ border-color: var(--ara-boolean-control-detail--hover);
67
+ box-shadow: var(--ara-boolean-control-shadow--hover);
68
+ color: var(--ara-boolean-control-fg--hover);
69
+ }
70
+
71
+ .maratus__BooleanControl__booleanControl:focus-visible {
72
+ background-color: var(--ara-boolean-control-bg--focus);
73
+ border-color: var(--ara-boolean-control-detail--focus);
74
+ box-shadow: var(--ara-boolean-control-shadow--focus);
75
+ color: var(--ara-boolean-control-fg--focus);
76
+ outline-color: var(--ara-boolean-control-focus);
77
+ outline-offset: var(--ara-boolean-control-focus-offset);
78
+ outline-width: var(--ara-boolean-control-focus-width);
79
+ }
80
+
81
+ .maratus__BooleanControl__booleanControl:checked,
82
+ .maratus__BooleanControl__booleanControl[aria-checked='true'] {
83
+ background-color: var(--ara-boolean-control-bg--checked);
84
+ border-color: var(--ara-boolean-control-detail--checked);
85
+ box-shadow: var(--ara-boolean-control-shadow--checked);
86
+ color: var(--ara-boolean-control-fg--checked);
87
+ }
88
+
89
+ .maratus__BooleanControl__booleanControl:disabled,
90
+ .maratus__BooleanControl__booleanControl[aria-disabled='true'] {
91
+ background-color: var(--ara-boolean-control-bg--disabled);
92
+ border-color: var(--ara-boolean-control-detail--disabled);
93
+ box-shadow: var(--ara-boolean-control-shadow--disabled);
94
+ color: var(--ara-boolean-control-fg--disabled);
95
+ }
96
+
97
+ .maratus__BooleanControl__booleanControl[aria-invalid='true'] {
98
+ background-color: var(--ara-boolean-control-bg--invalid);
99
+ border-color: var(--ara-boolean-control-detail--invalid);
100
+ box-shadow: var(--ara-boolean-control-shadow--invalid);
101
+ color: var(--ara-boolean-control-fg--invalid);
102
+ }
103
+
104
+ .maratus__BooleanControl__booleanControl[data-loading] {
105
+ background-color: var(--ara-boolean-control-bg--loading);
106
+ border-color: var(--ara-boolean-control-detail--loading);
107
+ box-shadow: var(--ara-boolean-control-shadow--loading);
108
+ color: var(--ara-boolean-control-fg--loading);
109
+ }
110
+
111
+ .maratus__BooleanControl__booleanControl[aria-readonly='true'] {
112
+ background-color: var(--ara-boolean-control-bg--readonly);
113
+ border-color: var(--ara-boolean-control-detail--readonly);
114
+ box-shadow: var(--ara-boolean-control-shadow--readonly);
115
+ color: var(--ara-boolean-control-fg--readonly);
116
+ }
117
+ }
@@ -1,9 +1,11 @@
1
- import type { BooleanControlProps } from './BooleanControl.types'
1
+ import type {
2
+ BooleanControlMode,
3
+ BooleanControlProps,
4
+ } from './BooleanControl.types'
2
5
  import { useBooleanControl } from './useBooleanControl'
3
6
 
4
- export function BooleanControl(props: BooleanControlProps) {
5
- const { children, ...hookProps } = props
6
- const control = useBooleanControl(hookProps)
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 { AriaAttributes, ReactNode } from 'react'
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 BooleanControlRenderProps = FieldControlRenderProps &
10
- ValidityHandlerProps & {
11
- 'aria-checked'?: AriaAttributes['aria-checked']
12
- checked?: boolean
13
- disabled?: boolean
14
- name?: string
15
- required?: boolean
16
- role?: BooleanControlRole
17
- type?: 'checkbox'
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 BooleanControlRenderArgs = {
21
- booleanControlProps: BooleanControlRenderProps
22
- }
23
-
24
- export type UseBooleanControlOptions = Omit<
25
- BooleanControlRenderProps,
26
- | 'aria-busy'
27
- | 'aria-checked'
28
- | 'aria-describedby'
29
- | 'aria-disabled'
30
- | 'aria-errormessage'
31
- | 'aria-invalid'
32
- | 'aria-readonly'
33
- | 'aria-required'
34
- | 'children'
35
- | 'data-loading'
36
- | 'disabled'
37
- | 'id'
38
- | 'name'
39
- | 'required'
40
- | 'role'
41
- | 'type'
42
- | keyof ValidityHandlerProps
43
- > &
44
- Partial<ValidityHandlerProps> & {
45
- role?: BooleanControlRole
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
- export type UseBooleanControlResult = {
49
- booleanControlProps: BooleanControlRenderProps
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 BooleanControlProps = UseBooleanControlOptions & {
53
- children: (props: BooleanControlRenderArgs) => ReactNode
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
+ }
@@ -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
- BooleanControlRenderProps,
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: UseBooleanControlOptions = {},
16
- ): UseBooleanControlResult {
17
- const { role, ...fieldControlOptions } = options
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
- useFieldControl(fieldControlOptions)
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
- if (role) {
28
- return {
29
- booleanControlProps: {
30
- ...controlProps,
31
- ...(field.isRequired ? { 'aria-required': true } : {}),
61
+ return mode === 'custom'
62
+ ? getCustomBooleanControl({
63
+ checked,
64
+ controlProps,
65
+ disabledState,
66
+ field,
32
67
  role,
33
- },
34
- }
35
- }
36
-
37
- return {
38
- booleanControlProps: getNativeBooleanControlProps(controlProps, field),
39
- }
68
+ })
69
+ : getNativeBooleanControl({
70
+ checked,
71
+ controlProps,
72
+ disabledState,
73
+ field,
74
+ role,
75
+ })
40
76
  }
41
77
 
42
- function getNativeBooleanControlProps(
43
- fieldControlProps: FieldControlRenderProps & ValidityHandlerProps,
44
- field: FieldContextValue,
45
- ): BooleanControlRenderProps {
46
- const booleanControlProps = { ...fieldControlProps }
47
-
48
- delete booleanControlProps['aria-readonly']
49
- delete booleanControlProps['aria-required']
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
  }