@hanzogui/checkbox-headless 2.0.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.
Files changed (58) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/BubbleInput.cjs +82 -0
  3. package/dist/cjs/BubbleInput.native.js +86 -0
  4. package/dist/cjs/BubbleInput.native.js.map +1 -0
  5. package/dist/cjs/index.cjs +19 -0
  6. package/dist/cjs/index.native.js +22 -0
  7. package/dist/cjs/index.native.js.map +1 -0
  8. package/dist/cjs/useCheckbox.cjs +96 -0
  9. package/dist/cjs/useCheckbox.native.js +106 -0
  10. package/dist/cjs/useCheckbox.native.js.map +1 -0
  11. package/dist/cjs/utils.cjs +32 -0
  12. package/dist/cjs/utils.native.js +35 -0
  13. package/dist/cjs/utils.native.js.map +1 -0
  14. package/dist/esm/BubbleInput.mjs +48 -0
  15. package/dist/esm/BubbleInput.mjs.map +1 -0
  16. package/dist/esm/BubbleInput.native.js +49 -0
  17. package/dist/esm/BubbleInput.native.js.map +1 -0
  18. package/dist/esm/index.js +3 -0
  19. package/dist/esm/index.js.map +1 -0
  20. package/dist/esm/index.mjs +3 -0
  21. package/dist/esm/index.mjs.map +1 -0
  22. package/dist/esm/index.native.js +3 -0
  23. package/dist/esm/index.native.js.map +1 -0
  24. package/dist/esm/useCheckbox.mjs +62 -0
  25. package/dist/esm/useCheckbox.mjs.map +1 -0
  26. package/dist/esm/useCheckbox.native.js +69 -0
  27. package/dist/esm/useCheckbox.native.js.map +1 -0
  28. package/dist/esm/utils.mjs +8 -0
  29. package/dist/esm/utils.mjs.map +1 -0
  30. package/dist/esm/utils.native.js +8 -0
  31. package/dist/esm/utils.native.js.map +1 -0
  32. package/dist/jsx/BubbleInput.mjs +48 -0
  33. package/dist/jsx/BubbleInput.mjs.map +1 -0
  34. package/dist/jsx/BubbleInput.native.js +86 -0
  35. package/dist/jsx/BubbleInput.native.js.map +1 -0
  36. package/dist/jsx/index.js +3 -0
  37. package/dist/jsx/index.js.map +1 -0
  38. package/dist/jsx/index.mjs +3 -0
  39. package/dist/jsx/index.mjs.map +1 -0
  40. package/dist/jsx/index.native.js +22 -0
  41. package/dist/jsx/index.native.js.map +1 -0
  42. package/dist/jsx/useCheckbox.mjs +62 -0
  43. package/dist/jsx/useCheckbox.mjs.map +1 -0
  44. package/dist/jsx/useCheckbox.native.js +106 -0
  45. package/dist/jsx/useCheckbox.native.js.map +1 -0
  46. package/dist/jsx/utils.mjs +8 -0
  47. package/dist/jsx/utils.mjs.map +1 -0
  48. package/dist/jsx/utils.native.js +35 -0
  49. package/dist/jsx/utils.native.js.map +1 -0
  50. package/package.json +60 -0
  51. package/src/BubbleInput.tsx +64 -0
  52. package/src/index.ts +2 -0
  53. package/src/useCheckbox.tsx +119 -0
  54. package/src/utils.tsx +9 -0
  55. package/types/BubbleInput.d.ts +10 -0
  56. package/types/index.d.ts +3 -0
  57. package/types/useCheckbox.d.ts +43 -0
  58. package/types/utils.d.ts +4 -0
package/package.json ADDED
@@ -0,0 +1,60 @@
1
+ {
2
+ "name": "@hanzogui/checkbox-headless",
3
+ "version": "2.0.0",
4
+ "gitHead": "a49cc7ea6b93ba384e77a4880ae48ac4a5635c14",
5
+ "source": "src/index.ts",
6
+ "files": [
7
+ "src",
8
+ "types",
9
+ "dist"
10
+ ],
11
+ "type": "module",
12
+ "sideEffects": [
13
+ "*.css"
14
+ ],
15
+ "main": "dist/cjs",
16
+ "module": "dist/esm",
17
+ "types": "./types/index.d.ts",
18
+ "exports": {
19
+ "./package.json": "./package.json",
20
+ ".": {
21
+ "types": "./types/index.d.ts",
22
+ "react-native": "./dist/esm/index.native.js",
23
+ "browser": "./dist/esm/index.mjs",
24
+ "module": "./dist/esm/index.mjs",
25
+ "import": "./dist/esm/index.mjs",
26
+ "require": "./dist/cjs/index.cjs",
27
+ "default": "./dist/esm/index.mjs"
28
+ }
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ },
33
+ "scripts": {
34
+ "build": "hanzo-gui-build",
35
+ "watch": "hanzo-gui-build --watch",
36
+ "clean": "hanzo-gui-build clean",
37
+ "clean:build": "hanzo-gui-build clean:build"
38
+ },
39
+ "dependencies": {
40
+ "@hanzogui/compose-refs": "2.0.0-rc.29",
41
+ "@hanzogui/constants": "2.0.0-rc.29",
42
+ "@hanzogui/create-context": "2.0.0-rc.29",
43
+ "@hanzogui/focusable": "2.0.0",
44
+ "@hanzogui/helpers": "2.0.0-rc.29",
45
+ "@hanzogui/label": "2.0.0",
46
+ "@hanzogui/use-controllable-state": "2.0.0-rc.29",
47
+ "@hanzogui/use-previous": "2.0.0-rc.29",
48
+ "@hanzogui/web": "2.0.0-rc.29"
49
+ },
50
+ "devDependencies": {
51
+ "@hanzogui/build": "2.0.0-rc.29",
52
+ "react": ">=19",
53
+ "react-native": "0.83.2"
54
+ },
55
+ "peerDependencies": {
56
+ "react": ">=19",
57
+ "react-native": "*"
58
+ },
59
+ "module:jsx": "dist/jsx"
60
+ }
@@ -0,0 +1,64 @@
1
+ import { usePrevious } from '@hanzogui/use-previous'
2
+ import * as React from 'react'
3
+
4
+ import type { CheckedState } from './useCheckbox'
5
+ import { isIndeterminate } from './utils'
6
+
7
+ export interface BubbleInputProps extends Omit<React.ComponentProps<'input'>, 'checked'> {
8
+ checked: CheckedState
9
+ control: HTMLElement | null
10
+ bubbles: boolean
11
+
12
+ isHidden?: boolean
13
+ }
14
+
15
+ export const BubbleInput = (props: BubbleInputProps) => {
16
+ const { checked, bubbles = true, control, isHidden, ...inputProps } = props
17
+ const ref = React.useRef<HTMLInputElement>(null)
18
+ const prevChecked = usePrevious(checked)
19
+ // const controlSize = useSize(control)
20
+ // Bubble checked change to parents (e.g form change event)
21
+ React.useEffect(() => {
22
+ const input = ref.current!
23
+ const inputProto = window.HTMLInputElement.prototype
24
+ const descriptor = Object.getOwnPropertyDescriptor(
25
+ inputProto,
26
+ 'checked'
27
+ ) as PropertyDescriptor
28
+ const setChecked = descriptor.set
29
+
30
+ if (prevChecked !== checked && setChecked) {
31
+ const event = new Event('click', { bubbles })
32
+ input.indeterminate = isIndeterminate(checked)
33
+ setChecked.call(input, isIndeterminate(checked) ? false : checked)
34
+ input.dispatchEvent(event)
35
+ }
36
+ }, [prevChecked, checked, bubbles])
37
+
38
+ return (
39
+ <input
40
+ type="checkbox"
41
+ defaultChecked={isIndeterminate(checked) ? false : checked}
42
+ {...inputProps}
43
+ tabIndex={-1}
44
+ ref={ref}
45
+ aria-hidden={isHidden}
46
+ style={{
47
+ ...(isHidden
48
+ ? {
49
+ // ...controlSize,
50
+ position: 'absolute',
51
+ pointerEvents: 'none',
52
+ opacity: 0,
53
+ margin: 0,
54
+ }
55
+ : {
56
+ appearance: 'auto',
57
+ accentColor: 'var(--color6)',
58
+ }),
59
+
60
+ ...props.style,
61
+ }}
62
+ />
63
+ )
64
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './useCheckbox'
2
+ export * from './utils'
@@ -0,0 +1,119 @@
1
+ import { useComposedRefs } from '@hanzogui/compose-refs'
2
+ import { isWeb } from '@hanzogui/constants'
3
+ import type { GestureReponderEvent } from '@hanzogui/web'
4
+ import { composeEventHandlers } from '@hanzogui/helpers'
5
+ import { useLabelContext } from '@hanzogui/label'
6
+ import React, { useMemo } from 'react'
7
+ import type { PressableProps, View, ViewProps } from 'react-native'
8
+
9
+ import { BubbleInput } from './BubbleInput'
10
+ import { getState, isIndeterminate } from './utils'
11
+
12
+ export type CheckedState = boolean | 'indeterminate'
13
+
14
+ type CheckboxBaseProps = Omit<ViewProps, 'onFocus' | 'onBlur'> &
15
+ Pick<PressableProps, 'onPress'>
16
+
17
+ export type CheckboxExtraProps = {
18
+ children?: React.ReactNode
19
+ id?: string
20
+ disabled?: boolean
21
+ checked?: CheckedState
22
+ defaultChecked?: CheckedState
23
+ required?: boolean
24
+ /**
25
+ *
26
+ * @param checked Either boolean or "indeterminate" which is meant to allow for a third state that means "neither", usually indicated by a minus sign.
27
+ */
28
+ onCheckedChange?(checked: CheckedState): void
29
+ labelledBy?: string
30
+ name?: string
31
+ value?: string
32
+ }
33
+
34
+ export type CheckboxProps = CheckboxBaseProps & CheckboxExtraProps
35
+
36
+ export function useCheckbox<R extends View, P extends CheckboxProps>(
37
+ props: P,
38
+ [checked, setChecked]: [
39
+ CheckedState,
40
+ React.Dispatch<React.SetStateAction<CheckedState>>,
41
+ ],
42
+ ref: React.Ref<R>
43
+ ) {
44
+ const {
45
+ labelledBy: ariaLabelledby,
46
+ name,
47
+ required,
48
+ disabled,
49
+ value = 'on',
50
+ onCheckedChange,
51
+ ...checkboxProps
52
+ } = props
53
+ const [button, setButton] = React.useState<HTMLButtonElement | null>(null)
54
+ const composedRefs = useComposedRefs(ref, setButton as any)
55
+ const hasConsumerStoppedPropagationRef = React.useRef(false)
56
+ // We set this to true by default so that events bubble to forms without JS (SSR)
57
+ const isFormControl = isWeb ? (button ? Boolean(button.closest('form')) : true) : false
58
+
59
+ const labelId = useLabelContext(button)
60
+ const labelledBy = ariaLabelledby || labelId
61
+
62
+ const parentKeyDown = (props as React.HTMLProps<HTMLButtonElement>).onKeyDown
63
+
64
+ const handleKeyDown = useMemo(
65
+ () =>
66
+ composeEventHandlers(parentKeyDown, (event) => {
67
+ // According to WAI ARIA, Checkboxes don't activate on enter keypress
68
+ if (event.key === 'Enter') event.preventDefault()
69
+ }),
70
+ [parentKeyDown]
71
+ )
72
+
73
+ const handlePress = useMemo(
74
+ () =>
75
+ composeEventHandlers(props.onPress as any, (event: GestureReponderEvent) => {
76
+ setChecked((prevChecked) => (isIndeterminate(prevChecked) ? true : !prevChecked))
77
+ if (isFormControl && 'isPropagationStopped' in event) {
78
+ hasConsumerStoppedPropagationRef.current = event.isPropagationStopped()
79
+ // if checkbox is in a form, stop propagation from the button so that we only propagate
80
+ // one click event (from the input). We propagate changes from an input so that native
81
+ // form validation works and form events reflect checkbox updates.
82
+ if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation()
83
+ }
84
+ }),
85
+ [isFormControl]
86
+ )
87
+
88
+ return {
89
+ bubbleInput:
90
+ isWeb && isFormControl ? (
91
+ <BubbleInput
92
+ isHidden
93
+ control={button}
94
+ bubbles={!hasConsumerStoppedPropagationRef.current}
95
+ name={name}
96
+ value={value}
97
+ checked={checked}
98
+ required={required}
99
+ disabled={disabled}
100
+ />
101
+ ) : null,
102
+ checkboxRef: composedRefs,
103
+ checkboxProps: {
104
+ role: 'checkbox',
105
+ 'aria-labelledby': labelledBy,
106
+ 'aria-checked': isIndeterminate(checked) ? 'mixed' : checked,
107
+ ...checkboxProps,
108
+ ...(isWeb && {
109
+ type: 'button',
110
+ value,
111
+ 'data-state': getState(checked),
112
+ 'data-disabled': disabled ? '' : undefined,
113
+ disabled: disabled,
114
+ onKeyDown: disabled ? undefined : handleKeyDown,
115
+ }),
116
+ onPress: disabled ? undefined : handlePress,
117
+ },
118
+ }
119
+ }
package/src/utils.tsx ADDED
@@ -0,0 +1,9 @@
1
+ import type { CheckedState } from './useCheckbox'
2
+
3
+ export function isIndeterminate(checked?: CheckedState): checked is 'indeterminate' {
4
+ return checked === 'indeterminate'
5
+ }
6
+
7
+ export function getState(checked: CheckedState) {
8
+ return isIndeterminate(checked) ? 'indeterminate' : checked ? 'checked' : 'unchecked'
9
+ }
@@ -0,0 +1,10 @@
1
+ import * as React from 'react';
2
+ import type { CheckedState } from './useCheckbox';
3
+ export interface BubbleInputProps extends Omit<React.ComponentProps<'input'>, 'checked'> {
4
+ checked: CheckedState;
5
+ control: HTMLElement | null;
6
+ bubbles: boolean;
7
+ isHidden?: boolean;
8
+ }
9
+ export declare const BubbleInput: (props: BubbleInputProps) => import("react/jsx-runtime").JSX.Element;
10
+ //# sourceMappingURL=BubbleInput.d.ts.map
@@ -0,0 +1,3 @@
1
+ export * from './useCheckbox';
2
+ export * from './utils';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1,43 @@
1
+ import React from 'react';
2
+ import type { PressableProps, View, ViewProps } from 'react-native';
3
+ export type CheckedState = boolean | 'indeterminate';
4
+ type CheckboxBaseProps = Omit<ViewProps, 'onFocus' | 'onBlur'> & Pick<PressableProps, 'onPress'>;
5
+ export type CheckboxExtraProps = {
6
+ children?: React.ReactNode;
7
+ id?: string;
8
+ disabled?: boolean;
9
+ checked?: CheckedState;
10
+ defaultChecked?: CheckedState;
11
+ required?: boolean;
12
+ /**
13
+ *
14
+ * @param checked Either boolean or "indeterminate" which is meant to allow for a third state that means "neither", usually indicated by a minus sign.
15
+ */
16
+ onCheckedChange?(checked: CheckedState): void;
17
+ labelledBy?: string;
18
+ name?: string;
19
+ value?: string;
20
+ };
21
+ export type CheckboxProps = CheckboxBaseProps & CheckboxExtraProps;
22
+ export declare function useCheckbox<R extends View, P extends CheckboxProps>(props: P, [checked, setChecked]: [
23
+ CheckedState,
24
+ React.Dispatch<React.SetStateAction<CheckedState>>
25
+ ], ref: React.Ref<R>): {
26
+ bubbleInput: import("react/jsx-runtime").JSX.Element | null;
27
+ checkboxRef: (node: R | null) => void;
28
+ checkboxProps: {
29
+ role: string;
30
+ 'aria-labelledby': string | undefined;
31
+ 'aria-checked': string | boolean;
32
+ } & Omit<P, "disabled" | "labelledBy" | "name" | "required" | "value" | "onCheckedChange"> & {
33
+ onPress: import("@gui/web").EventHandler<import("react-native").GestureResponderEvent> | undefined;
34
+ type?: string | undefined;
35
+ value?: string | undefined;
36
+ 'data-state'?: string | undefined;
37
+ 'data-disabled'?: string | undefined;
38
+ disabled?: boolean | undefined;
39
+ onKeyDown?: import("@gui/web").EventHandler<React.KeyboardEvent<HTMLButtonElement>> | undefined;
40
+ };
41
+ };
42
+ export {};
43
+ //# sourceMappingURL=useCheckbox.d.ts.map
@@ -0,0 +1,4 @@
1
+ import type { CheckedState } from './useCheckbox';
2
+ export declare function isIndeterminate(checked?: CheckedState): checked is 'indeterminate';
3
+ export declare function getState(checked: CheckedState): "indeterminate" | "checked" | "unchecked";
4
+ //# sourceMappingURL=utils.d.ts.map