@radix-ui/react-checkbox 1.0.4 → 1.1.0-rc.1
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/dist/index.d.mts +26 -14
- package/dist/index.d.ts +26 -14
- package/dist/index.js +171 -172
- package/dist/index.js.map +7 -1
- package/dist/index.mjs +150 -165
- package/dist/index.mjs.map +7 -1
- package/package.json +11 -11
- package/dist/index.d.ts.map +0 -1
package/dist/index.d.mts
CHANGED
|
@@ -1,26 +1,38 @@
|
|
|
1
|
-
import * as React from
|
|
2
|
-
import * as Radix from
|
|
3
|
-
import { Primitive } from
|
|
4
|
-
|
|
5
|
-
type
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import * as Radix from '@radix-ui/react-primitive';
|
|
3
|
+
import { Primitive } from '@radix-ui/react-primitive';
|
|
4
|
+
|
|
5
|
+
declare type Scope<C = any> = {
|
|
6
|
+
[scopeName: string]: React.Context<C>[];
|
|
7
|
+
} | undefined;
|
|
8
|
+
declare type ScopeHook = (scope: Scope) => {
|
|
9
|
+
[__scopeProp: string]: Scope;
|
|
10
|
+
};
|
|
11
|
+
interface CreateScope {
|
|
12
|
+
scopeName: string;
|
|
13
|
+
(): ScopeHook;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare const createCheckboxScope: CreateScope;
|
|
17
|
+
declare type CheckedState = boolean | 'indeterminate';
|
|
18
|
+
declare type PrimitiveButtonProps = Radix.ComponentPropsWithoutRef<typeof Primitive.button>;
|
|
19
|
+
interface CheckboxProps extends Omit<PrimitiveButtonProps, 'checked' | 'defaultChecked'> {
|
|
8
20
|
checked?: CheckedState;
|
|
9
21
|
defaultChecked?: CheckedState;
|
|
10
22
|
required?: boolean;
|
|
11
23
|
onCheckedChange?(checked: CheckedState): void;
|
|
12
24
|
}
|
|
13
|
-
|
|
14
|
-
type PrimitiveSpanProps = Radix.ComponentPropsWithoutRef<typeof Primitive.span>;
|
|
15
|
-
|
|
25
|
+
declare const Checkbox: React.ForwardRefExoticComponent<CheckboxProps & React.RefAttributes<HTMLButtonElement>>;
|
|
26
|
+
declare type PrimitiveSpanProps = Radix.ComponentPropsWithoutRef<typeof Primitive.span>;
|
|
27
|
+
interface CheckboxIndicatorProps extends PrimitiveSpanProps {
|
|
16
28
|
/**
|
|
17
29
|
* Used to force mounting when more control is needed. Useful when
|
|
18
30
|
* controlling animation with React animation libraries.
|
|
19
31
|
*/
|
|
20
32
|
forceMount?: true;
|
|
21
33
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
34
|
+
declare const CheckboxIndicator: React.ForwardRefExoticComponent<CheckboxIndicatorProps & React.RefAttributes<HTMLSpanElement>>;
|
|
35
|
+
declare const Root: React.ForwardRefExoticComponent<CheckboxProps & React.RefAttributes<HTMLButtonElement>>;
|
|
36
|
+
declare const Indicator: React.ForwardRefExoticComponent<CheckboxIndicatorProps & React.RefAttributes<HTMLSpanElement>>;
|
|
25
37
|
|
|
26
|
-
|
|
38
|
+
export { Checkbox, CheckboxIndicator, type CheckboxIndicatorProps, type CheckboxProps, Indicator, Root, createCheckboxScope };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,26 +1,38 @@
|
|
|
1
|
-
import * as React from
|
|
2
|
-
import * as Radix from
|
|
3
|
-
import { Primitive } from
|
|
4
|
-
|
|
5
|
-
type
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import * as Radix from '@radix-ui/react-primitive';
|
|
3
|
+
import { Primitive } from '@radix-ui/react-primitive';
|
|
4
|
+
|
|
5
|
+
declare type Scope<C = any> = {
|
|
6
|
+
[scopeName: string]: React.Context<C>[];
|
|
7
|
+
} | undefined;
|
|
8
|
+
declare type ScopeHook = (scope: Scope) => {
|
|
9
|
+
[__scopeProp: string]: Scope;
|
|
10
|
+
};
|
|
11
|
+
interface CreateScope {
|
|
12
|
+
scopeName: string;
|
|
13
|
+
(): ScopeHook;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
declare const createCheckboxScope: CreateScope;
|
|
17
|
+
declare type CheckedState = boolean | 'indeterminate';
|
|
18
|
+
declare type PrimitiveButtonProps = Radix.ComponentPropsWithoutRef<typeof Primitive.button>;
|
|
19
|
+
interface CheckboxProps extends Omit<PrimitiveButtonProps, 'checked' | 'defaultChecked'> {
|
|
8
20
|
checked?: CheckedState;
|
|
9
21
|
defaultChecked?: CheckedState;
|
|
10
22
|
required?: boolean;
|
|
11
23
|
onCheckedChange?(checked: CheckedState): void;
|
|
12
24
|
}
|
|
13
|
-
|
|
14
|
-
type PrimitiveSpanProps = Radix.ComponentPropsWithoutRef<typeof Primitive.span>;
|
|
15
|
-
|
|
25
|
+
declare const Checkbox: React.ForwardRefExoticComponent<CheckboxProps & React.RefAttributes<HTMLButtonElement>>;
|
|
26
|
+
declare type PrimitiveSpanProps = Radix.ComponentPropsWithoutRef<typeof Primitive.span>;
|
|
27
|
+
interface CheckboxIndicatorProps extends PrimitiveSpanProps {
|
|
16
28
|
/**
|
|
17
29
|
* Used to force mounting when more control is needed. Useful when
|
|
18
30
|
* controlling animation with React animation libraries.
|
|
19
31
|
*/
|
|
20
32
|
forceMount?: true;
|
|
21
33
|
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
34
|
+
declare const CheckboxIndicator: React.ForwardRefExoticComponent<CheckboxIndicatorProps & React.RefAttributes<HTMLSpanElement>>;
|
|
35
|
+
declare const Root: React.ForwardRefExoticComponent<CheckboxProps & React.RefAttributes<HTMLButtonElement>>;
|
|
36
|
+
declare const Indicator: React.ForwardRefExoticComponent<CheckboxIndicatorProps & React.RefAttributes<HTMLSpanElement>>;
|
|
25
37
|
|
|
26
|
-
|
|
38
|
+
export { Checkbox, CheckboxIndicator, type CheckboxIndicatorProps, type CheckboxProps, Indicator, Root, createCheckboxScope };
|
package/dist/index.js
CHANGED
|
@@ -1,185 +1,184 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
var
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
var
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
(() => {
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
10
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
11
|
+
}) : x)(function(x) {
|
|
12
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
13
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
14
|
+
});
|
|
15
|
+
var __copyProps = (to, from, except, desc) => {
|
|
16
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
17
|
+
for (let key of __getOwnPropNames(from))
|
|
18
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
19
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
20
|
+
}
|
|
21
|
+
return to;
|
|
22
|
+
};
|
|
23
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
24
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
25
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
26
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
27
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
28
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
29
|
+
mod
|
|
30
|
+
));
|
|
31
|
+
|
|
32
|
+
// packages/react/checkbox/src/Checkbox.tsx
|
|
33
|
+
var React = __toESM(__require("react"));
|
|
34
|
+
var import_react_compose_refs = __require("@radix-ui/react-compose-refs");
|
|
35
|
+
var import_react_context = __require("@radix-ui/react-context");
|
|
36
|
+
var import_primitive = __require("@radix-ui/primitive");
|
|
37
|
+
var import_react_use_controllable_state = __require("@radix-ui/react-use-controllable-state");
|
|
38
|
+
var import_react_use_previous = __require("@radix-ui/react-use-previous");
|
|
39
|
+
var import_react_use_size = __require("@radix-ui/react-use-size");
|
|
40
|
+
var import_react_presence = __require("@radix-ui/react-presence");
|
|
41
|
+
var import_react_primitive = __require("@radix-ui/react-primitive");
|
|
42
|
+
var import_jsx_runtime = __require("react/jsx-runtime");
|
|
43
|
+
var CHECKBOX_NAME = "Checkbox";
|
|
44
|
+
var [createCheckboxContext, createCheckboxScope] = (0, import_react_context.createContextScope)(CHECKBOX_NAME);
|
|
45
|
+
var [CheckboxProvider, useCheckboxContext] = createCheckboxContext(CHECKBOX_NAME);
|
|
46
|
+
var Checkbox = React.forwardRef(
|
|
47
|
+
(props, forwardedRef) => {
|
|
48
|
+
const {
|
|
49
|
+
__scopeCheckbox,
|
|
50
|
+
name,
|
|
51
|
+
checked: checkedProp,
|
|
52
|
+
defaultChecked,
|
|
53
|
+
required,
|
|
54
|
+
disabled,
|
|
55
|
+
value = "on",
|
|
56
|
+
onCheckedChange,
|
|
57
|
+
...checkboxProps
|
|
58
|
+
} = props;
|
|
59
|
+
const [button, setButton] = React.useState(null);
|
|
60
|
+
const composedRefs = (0, import_react_compose_refs.useComposedRefs)(forwardedRef, (node) => setButton(node));
|
|
61
|
+
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
|
62
|
+
const isFormControl = button ? Boolean(button.closest("form")) : true;
|
|
63
|
+
const [checked = false, setChecked] = (0, import_react_use_controllable_state.useControllableState)({
|
|
47
64
|
prop: checkedProp,
|
|
48
65
|
defaultProp: defaultChecked,
|
|
49
66
|
onChange: onCheckedChange
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const form = button
|
|
67
|
+
});
|
|
68
|
+
const initialCheckedStateRef = React.useRef(checked);
|
|
69
|
+
React.useEffect(() => {
|
|
70
|
+
const form = button?.form;
|
|
54
71
|
if (form) {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
return ()=>form.removeEventListener('reset', reset)
|
|
59
|
-
;
|
|
72
|
+
const reset = () => setChecked(initialCheckedStateRef.current);
|
|
73
|
+
form.addEventListener("reset", reset);
|
|
74
|
+
return () => form.removeEventListener("reset", reset);
|
|
60
75
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
}),
|
|
84
|
-
onClick: $dKOox$radixuiprimitive.composeEventHandlers(props.onClick, (event)=>{
|
|
85
|
-
setChecked((prevChecked)=>$a17f2c9fbf2ccf8c$var$isIndeterminate(prevChecked) ? true : !prevChecked
|
|
86
|
-
);
|
|
87
|
-
if (isFormControl) {
|
|
88
|
-
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped(); // if checkbox is in a form, stop propagation from the button so that we only propagate
|
|
89
|
-
// one click event (from the input). We propagate changes from an input so that native
|
|
90
|
-
// form validation works and form events reflect checkbox updates.
|
|
76
|
+
}, [button, setChecked]);
|
|
77
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(CheckboxProvider, { scope: __scopeCheckbox, state: checked, disabled, children: [
|
|
78
|
+
/* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
79
|
+
import_react_primitive.Primitive.button,
|
|
80
|
+
{
|
|
81
|
+
type: "button",
|
|
82
|
+
role: "checkbox",
|
|
83
|
+
"aria-checked": isIndeterminate(checked) ? "mixed" : checked,
|
|
84
|
+
"aria-required": required,
|
|
85
|
+
"data-state": getState(checked),
|
|
86
|
+
"data-disabled": disabled ? "" : void 0,
|
|
87
|
+
disabled,
|
|
88
|
+
value,
|
|
89
|
+
...checkboxProps,
|
|
90
|
+
ref: composedRefs,
|
|
91
|
+
onKeyDown: (0, import_primitive.composeEventHandlers)(props.onKeyDown, (event) => {
|
|
92
|
+
if (event.key === "Enter") event.preventDefault();
|
|
93
|
+
}),
|
|
94
|
+
onClick: (0, import_primitive.composeEventHandlers)(props.onClick, (event) => {
|
|
95
|
+
setChecked((prevChecked) => isIndeterminate(prevChecked) ? true : !prevChecked);
|
|
96
|
+
if (isFormControl) {
|
|
97
|
+
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
91
98
|
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
...props.style
|
|
127
|
-
}
|
|
128
|
-
})));
|
|
129
|
-
});
|
|
130
|
-
/*#__PURE__*/ Object.assign($a17f2c9fbf2ccf8c$export$59aad738f51d1c05, {
|
|
131
|
-
displayName: $a17f2c9fbf2ccf8c$var$INDICATOR_NAME
|
|
132
|
-
});
|
|
133
|
-
/* ---------------------------------------------------------------------------------------------- */ const $a17f2c9fbf2ccf8c$var$BubbleInput = (props)=>{
|
|
134
|
-
const { control: control , checked: checked , bubbles: bubbles = true , ...inputProps } = props;
|
|
135
|
-
const ref = $dKOox$react.useRef(null);
|
|
136
|
-
const prevChecked = $dKOox$radixuireactuseprevious.usePrevious(checked);
|
|
137
|
-
const controlSize = $dKOox$radixuireactusesize.useSize(control); // Bubble checked change to parents (e.g form change event)
|
|
138
|
-
$dKOox$react.useEffect(()=>{
|
|
139
|
-
const input = ref.current;
|
|
140
|
-
const inputProto = window.HTMLInputElement.prototype;
|
|
141
|
-
const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked');
|
|
142
|
-
const setChecked = descriptor.set;
|
|
143
|
-
if (prevChecked !== checked && setChecked) {
|
|
144
|
-
const event = new Event('click', {
|
|
145
|
-
bubbles: bubbles
|
|
146
|
-
});
|
|
147
|
-
input.indeterminate = $a17f2c9fbf2ccf8c$var$isIndeterminate(checked);
|
|
148
|
-
setChecked.call(input, $a17f2c9fbf2ccf8c$var$isIndeterminate(checked) ? false : checked);
|
|
149
|
-
input.dispatchEvent(event);
|
|
99
|
+
}
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
),
|
|
103
|
+
isFormControl && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
104
|
+
BubbleInput,
|
|
105
|
+
{
|
|
106
|
+
control: button,
|
|
107
|
+
bubbles: !hasConsumerStoppedPropagationRef.current,
|
|
108
|
+
name,
|
|
109
|
+
value,
|
|
110
|
+
checked,
|
|
111
|
+
required,
|
|
112
|
+
disabled,
|
|
113
|
+
style: { transform: "translateX(-100%)" }
|
|
114
|
+
}
|
|
115
|
+
)
|
|
116
|
+
] });
|
|
117
|
+
}
|
|
118
|
+
);
|
|
119
|
+
Checkbox.displayName = CHECKBOX_NAME;
|
|
120
|
+
var INDICATOR_NAME = "CheckboxIndicator";
|
|
121
|
+
var CheckboxIndicator = React.forwardRef(
|
|
122
|
+
(props, forwardedRef) => {
|
|
123
|
+
const { __scopeCheckbox, forceMount, ...indicatorProps } = props;
|
|
124
|
+
const context = useCheckboxContext(INDICATOR_NAME, __scopeCheckbox);
|
|
125
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_react_presence.Presence, { present: forceMount || isIndeterminate(context.state) || context.state === true, children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
126
|
+
import_react_primitive.Primitive.span,
|
|
127
|
+
{
|
|
128
|
+
"data-state": getState(context.state),
|
|
129
|
+
"data-disabled": context.disabled ? "" : void 0,
|
|
130
|
+
...indicatorProps,
|
|
131
|
+
ref: forwardedRef,
|
|
132
|
+
style: { pointerEvents: "none", ...props.style }
|
|
150
133
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
134
|
+
) });
|
|
135
|
+
}
|
|
136
|
+
);
|
|
137
|
+
CheckboxIndicator.displayName = INDICATOR_NAME;
|
|
138
|
+
var BubbleInput = (props) => {
|
|
139
|
+
const { control, checked, bubbles = true, ...inputProps } = props;
|
|
140
|
+
const ref = React.useRef(null);
|
|
141
|
+
const prevChecked = (0, import_react_use_previous.usePrevious)(checked);
|
|
142
|
+
const controlSize = (0, import_react_use_size.useSize)(control);
|
|
143
|
+
React.useEffect(() => {
|
|
144
|
+
const input = ref.current;
|
|
145
|
+
const inputProto = window.HTMLInputElement.prototype;
|
|
146
|
+
const descriptor = Object.getOwnPropertyDescriptor(inputProto, "checked");
|
|
147
|
+
const setChecked = descriptor.set;
|
|
148
|
+
if (prevChecked !== checked && setChecked) {
|
|
149
|
+
const event = new Event("click", { bubbles });
|
|
150
|
+
input.indeterminate = isIndeterminate(checked);
|
|
151
|
+
setChecked.call(input, isIndeterminate(checked) ? false : checked);
|
|
152
|
+
input.dispatchEvent(event);
|
|
153
|
+
}
|
|
154
|
+
}, [prevChecked, checked, bubbles]);
|
|
155
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
156
|
+
"input",
|
|
157
|
+
{
|
|
157
158
|
type: "checkbox",
|
|
158
159
|
"aria-hidden": true,
|
|
159
|
-
defaultChecked:
|
|
160
|
-
|
|
160
|
+
defaultChecked: isIndeterminate(checked) ? false : checked,
|
|
161
|
+
...inputProps,
|
|
161
162
|
tabIndex: -1,
|
|
162
|
-
ref
|
|
163
|
+
ref,
|
|
163
164
|
style: {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
165
|
+
...props.style,
|
|
166
|
+
...controlSize,
|
|
167
|
+
position: "absolute",
|
|
168
|
+
pointerEvents: "none",
|
|
169
|
+
opacity: 0,
|
|
170
|
+
margin: 0
|
|
170
171
|
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
172
|
+
}
|
|
173
|
+
);
|
|
174
|
+
};
|
|
175
|
+
function isIndeterminate(checked) {
|
|
176
|
+
return checked === "indeterminate";
|
|
177
|
+
}
|
|
178
|
+
function getState(checked) {
|
|
179
|
+
return isIndeterminate(checked) ? "indeterminate" : checked ? "checked" : "unchecked";
|
|
180
|
+
}
|
|
181
|
+
var Root = Checkbox;
|
|
182
|
+
var Indicator = CheckboxIndicator;
|
|
183
|
+
})();
|
|
185
184
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;;;;;;;;;;;A;;;;;;;;;;ACaA;;oGAEA,CAEA,MAAMc,mCAAa,GAAG,UAAtB,AAAA;AAGA,MAAM,CAACC,2CAAD,EAAwBf,yCAAxB,CAAA,GAA+CO,6CAAkB,CAACO,mCAAD,CAAvE,AAAA;AASA,MAAM,CAACE,sCAAD,EAAmBC,wCAAnB,CAAA,GACJF,2CAAqB,CAAuBD,mCAAvB,CADvB,AAAA;AAYA,MAAMb,yCAAQ,GAAA,aAAGI,CAAAA,uBAAA,CACf,CAACc,KAAD,EAAoCC,YAApC,GAAqD;IACnD,MAAM,E,iBACJC,eADI,CAAA,E,MAEJC,IAFI,CAAA,EAGJC,OAAO,EAAEC,WAHL,CAAA,E,gBAIJC,cAJI,CAAA,E,UAKJC,QALI,CAAA,E,UAMJC,QANI,CAAA,SAOJC,KAAK,GAAG,IAPJ,G,iBAQJC,eARI,CAAA,EASJ,GAAGC,aAAH,EATI,GAUFX,KAVJ,AAAM;IAWN,MAAM,CAACY,MAAD,EAASC,SAAT,CAAA,GAAsB3B,qBAAA,CAAyC,IAAzC,CAA5B,AAAA;IACA,MAAM6B,YAAY,GAAG5B,8CAAe,CAACc,YAAD,EAAgBe,CAAAA,IAAD,GAAUH,SAAS,CAACG,IAAD,CAAlC;IAAA,CAApC,AAAA;IACA,MAAMC,gCAAgC,GAAG/B,mBAAA,CAAa,KAAb,CAAzC,AAdmD,EAenD,iFADA;IAEA,MAAMiC,aAAa,GAAGP,MAAM,GAAGQ,OAAO,CAACR,MAAM,CAACS,OAAP,CAAe,MAAf,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KADJ;YAEL,GAAGM,WAFE;YAGLc,QAAQ,EAAE,UAHL;YAILrB,aAAa,EAAE,MAJV;YAKLsB,OAAO,EAAE,CALJ;YAMLC,MAAM,EAAE,CAARA;SANK;KAPT,CAAA,CADF,CACE;CAtBJ,AAuCC;AAED,SAASxC,qCAAT,CAAyB7B,OAAzB,EAA6E;IAC3E,OAAOA,OAAO,KAAK,eAAnB,CAAA;CACD;AAED,SAAS8B,8BAAT,CAAkB9B,OAAlB,EAAyC;IACvC,OAAO6B,qCAAe,CAAC7B,OAAD,CAAf,GAA2B,eAA3B,GAA6CA,OAAO,GAAG,SAAH,GAAe,WAA1E,CAAA;CACD;AAED,MAAMpB,yCAAI,GAAGF,yCAAb,AAAA;AACA,MAAMG,yCAAS,GAAGF,yCAAlB,AAAA;;AD3NA","sources":["packages/react/checkbox/src/index.ts","packages/react/checkbox/src/Checkbox.tsx"],"sourcesContent":["export {\n createCheckboxScope,\n //\n Checkbox,\n CheckboxIndicator,\n //\n Root,\n Indicator,\n} from './Checkbox';\nexport type { CheckboxProps, CheckboxIndicatorProps } from './Checkbox';\n","import * as React from 'react';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { composeEventHandlers } from '@radix-ui/primitive';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\nimport { usePrevious } from '@radix-ui/react-use-previous';\nimport { useSize } from '@radix-ui/react-use-size';\nimport { Presence } from '@radix-ui/react-presence';\nimport { Primitive } from '@radix-ui/react-primitive';\n\nimport type * as Radix from '@radix-ui/react-primitive';\nimport type { Scope } from '@radix-ui/react-context';\n\n/* -------------------------------------------------------------------------------------------------\n * Checkbox\n * -----------------------------------------------------------------------------------------------*/\n\nconst CHECKBOX_NAME = 'Checkbox';\n\ntype ScopedProps<P> = P & { __scopeCheckbox?: Scope };\nconst [createCheckboxContext, createCheckboxScope] = createContextScope(CHECKBOX_NAME);\n\ntype CheckedState = boolean | 'indeterminate';\n\ntype CheckboxContextValue = {\n state: CheckedState;\n disabled?: boolean;\n};\n\nconst [CheckboxProvider, useCheckboxContext] =\n createCheckboxContext<CheckboxContextValue>(CHECKBOX_NAME);\n\ntype CheckboxElement = React.ElementRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = Radix.ComponentPropsWithoutRef<typeof Primitive.button>;\ninterface CheckboxProps extends Omit<PrimitiveButtonProps, 'checked' | 'defaultChecked'> {\n checked?: CheckedState;\n defaultChecked?: CheckedState;\n required?: boolean;\n onCheckedChange?(checked: CheckedState): void;\n}\n\nconst Checkbox = React.forwardRef<CheckboxElement, CheckboxProps>(\n (props: ScopedProps<CheckboxProps>, forwardedRef) => {\n const {\n __scopeCheckbox,\n name,\n checked: checkedProp,\n defaultChecked,\n required,\n disabled,\n value = 'on',\n onCheckedChange,\n ...checkboxProps\n } = props;\n const [button, setButton] = React.useState<HTMLButtonElement | null>(null);\n const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));\n const hasConsumerStoppedPropagationRef = React.useRef(false);\n // We set this to true by default so that events bubble to forms without JS (SSR)\n const isFormControl = button ? Boolean(button.closest('form')) : true;\n const [checked = false, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked,\n onChange: onCheckedChange,\n });\n const initialCheckedStateRef = React.useRef(checked);\n React.useEffect(() => {\n const form = button?.form;\n if (form) {\n const reset = () => setChecked(initialCheckedStateRef.current);\n form.addEventListener('reset', reset);\n return () => form.removeEventListener('reset', reset);\n }\n }, [button, setChecked]);\n\n return (\n <CheckboxProvider scope={__scopeCheckbox} state={checked} disabled={disabled}>\n <Primitive.button\n type=\"button\"\n role=\"checkbox\"\n aria-checked={isIndeterminate(checked) ? 'mixed' : checked}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n value={value}\n {...checkboxProps}\n ref={composedRefs}\n onKeyDown={composeEventHandlers(props.onKeyDown, (event) => {\n // According to WAI ARIA, Checkboxes don't activate on enter keypress\n if (event.key === 'Enter') event.preventDefault();\n })}\n onClick={composeEventHandlers(props.onClick, (event) => {\n setChecked((prevChecked) => (isIndeterminate(prevChecked) ? true : !prevChecked));\n if (isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();\n // if checkbox is in a form, stop propagation from the button so that we only propagate\n // one click event (from the input). We propagate changes from an input so that native\n // form validation works and form events reflect checkbox updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();\n }\n })}\n />\n {isFormControl && (\n <BubbleInput\n control={button}\n bubbles={!hasConsumerStoppedPropagationRef.current}\n name={name}\n value={value}\n checked={checked}\n required={required}\n disabled={disabled}\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n style={{ transform: 'translateX(-100%)' }}\n />\n )}\n </CheckboxProvider>\n );\n }\n);\n\nCheckbox.displayName = CHECKBOX_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * CheckboxIndicator\n * -----------------------------------------------------------------------------------------------*/\n\nconst INDICATOR_NAME = 'CheckboxIndicator';\n\ntype CheckboxIndicatorElement = React.ElementRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = Radix.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface CheckboxIndicatorProps extends PrimitiveSpanProps {\n /**\n * Used to force mounting when more control is needed. Useful when\n * controlling animation with React animation libraries.\n */\n forceMount?: true;\n}\n\nconst CheckboxIndicator = React.forwardRef<CheckboxIndicatorElement, CheckboxIndicatorProps>(\n (props: ScopedProps<CheckboxIndicatorProps>, forwardedRef) => {\n const { __scopeCheckbox, forceMount, ...indicatorProps } = props;\n const context = useCheckboxContext(INDICATOR_NAME, __scopeCheckbox);\n return (\n <Presence present={forceMount || isIndeterminate(context.state) || context.state === true}>\n <Primitive.span\n data-state={getState(context.state)}\n data-disabled={context.disabled ? '' : undefined}\n {...indicatorProps}\n ref={forwardedRef}\n style={{ pointerEvents: 'none', ...props.style }}\n />\n </Presence>\n );\n }\n);\n\nCheckboxIndicator.displayName = INDICATOR_NAME;\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype InputProps = Radix.ComponentPropsWithoutRef<'input'>;\ninterface BubbleInputProps extends Omit<InputProps, 'checked'> {\n checked: CheckedState;\n control: HTMLElement | null;\n bubbles: boolean;\n}\n\nconst BubbleInput = (props: BubbleInputProps) => {\n const { control, checked, bubbles = true, ...inputProps } = props;\n const ref = React.useRef<HTMLInputElement>(null);\n const prevChecked = usePrevious(checked);\n const controlSize = useSize(control);\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = ref.current!;\n const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked') as PropertyDescriptor;\n const setChecked = descriptor.set;\n\n if (prevChecked !== checked && setChecked) {\n const event = new Event('click', { bubbles });\n input.indeterminate = isIndeterminate(checked);\n setChecked.call(input, isIndeterminate(checked) ? false : checked);\n input.dispatchEvent(event);\n }\n }, [prevChecked, checked, bubbles]);\n\n return (\n <input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={isIndeterminate(checked) ? false : checked}\n {...inputProps}\n tabIndex={-1}\n ref={ref}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n }}\n />\n );\n};\n\nfunction isIndeterminate(checked?: CheckedState): checked is 'indeterminate' {\n return checked === 'indeterminate';\n}\n\nfunction getState(checked: CheckedState) {\n return isIndeterminate(checked) ? 'indeterminate' : checked ? 'checked' : 'unchecked';\n}\n\nconst Root = Checkbox;\nconst Indicator = CheckboxIndicator;\n\nexport {\n createCheckboxScope,\n //\n Checkbox,\n CheckboxIndicator,\n //\n Root,\n Indicator,\n};\nexport type { CheckboxProps, CheckboxIndicatorProps };\n"],"names":["createCheckboxScope","Checkbox","CheckboxIndicator","Root","Indicator","React","useComposedRefs","createContextScope","composeEventHandlers","useControllableState","usePrevious","useSize","Presence","Primitive","CHECKBOX_NAME","createCheckboxContext","CheckboxProvider","useCheckboxContext","forwardRef","props","forwardedRef","__scopeCheckbox","name","checked","checkedProp","defaultChecked","required","disabled","value","onCheckedChange","checkboxProps","button","setButton","useState","composedRefs","node","hasConsumerStoppedPropagationRef","useRef","isFormControl","Boolean","closest","setChecked","prop","defaultProp","onChange","initialCheckedStateRef","useEffect","form","reset","current","addEventListener","removeEventListener","isIndeterminate","getState","undefined","onKeyDown","event","key","preventDefault","onClick","prevChecked","isPropagationStopped","stopPropagation","transform","INDICATOR_NAME","forceMount","indicatorProps","context","state","pointerEvents","style","BubbleInput","control","bubbles","inputProps","ref","controlSize","input","inputProto","window","HTMLInputElement","prototype","descriptor","Object","getOwnPropertyDescriptor","set","Event","indeterminate","call","dispatchEvent","position","opacity","margin"],"version":3,"file":"index.js.map"}
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/Checkbox.tsx"],
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { composeEventHandlers } from '@radix-ui/primitive';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\nimport { usePrevious } from '@radix-ui/react-use-previous';\nimport { useSize } from '@radix-ui/react-use-size';\nimport { Presence } from '@radix-ui/react-presence';\nimport { Primitive } from '@radix-ui/react-primitive';\n\nimport type * as Radix from '@radix-ui/react-primitive';\nimport type { Scope } from '@radix-ui/react-context';\n\n/* -------------------------------------------------------------------------------------------------\n * Checkbox\n * -----------------------------------------------------------------------------------------------*/\n\nconst CHECKBOX_NAME = 'Checkbox';\n\ntype ScopedProps<P> = P & { __scopeCheckbox?: Scope };\nconst [createCheckboxContext, createCheckboxScope] = createContextScope(CHECKBOX_NAME);\n\ntype CheckedState = boolean | 'indeterminate';\n\ntype CheckboxContextValue = {\n state: CheckedState;\n disabled?: boolean;\n};\n\nconst [CheckboxProvider, useCheckboxContext] =\n createCheckboxContext<CheckboxContextValue>(CHECKBOX_NAME);\n\ntype CheckboxElement = React.ElementRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = Radix.ComponentPropsWithoutRef<typeof Primitive.button>;\ninterface CheckboxProps extends Omit<PrimitiveButtonProps, 'checked' | 'defaultChecked'> {\n checked?: CheckedState;\n defaultChecked?: CheckedState;\n required?: boolean;\n onCheckedChange?(checked: CheckedState): void;\n}\n\nconst Checkbox = React.forwardRef<CheckboxElement, CheckboxProps>(\n (props: ScopedProps<CheckboxProps>, forwardedRef) => {\n const {\n __scopeCheckbox,\n name,\n checked: checkedProp,\n defaultChecked,\n required,\n disabled,\n value = 'on',\n onCheckedChange,\n ...checkboxProps\n } = props;\n const [button, setButton] = React.useState<HTMLButtonElement | null>(null);\n const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));\n const hasConsumerStoppedPropagationRef = React.useRef(false);\n // We set this to true by default so that events bubble to forms without JS (SSR)\n const isFormControl = button ? Boolean(button.closest('form')) : true;\n const [checked = false, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked,\n onChange: onCheckedChange,\n });\n const initialCheckedStateRef = React.useRef(checked);\n React.useEffect(() => {\n const form = button?.form;\n if (form) {\n const reset = () => setChecked(initialCheckedStateRef.current);\n form.addEventListener('reset', reset);\n return () => form.removeEventListener('reset', reset);\n }\n }, [button, setChecked]);\n\n return (\n <CheckboxProvider scope={__scopeCheckbox} state={checked} disabled={disabled}>\n <Primitive.button\n type=\"button\"\n role=\"checkbox\"\n aria-checked={isIndeterminate(checked) ? 'mixed' : checked}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n value={value}\n {...checkboxProps}\n ref={composedRefs}\n onKeyDown={composeEventHandlers(props.onKeyDown, (event) => {\n // According to WAI ARIA, Checkboxes don't activate on enter keypress\n if (event.key === 'Enter') event.preventDefault();\n })}\n onClick={composeEventHandlers(props.onClick, (event) => {\n setChecked((prevChecked) => (isIndeterminate(prevChecked) ? true : !prevChecked));\n if (isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();\n // if checkbox is in a form, stop propagation from the button so that we only propagate\n // one click event (from the input). We propagate changes from an input so that native\n // form validation works and form events reflect checkbox updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();\n }\n })}\n />\n {isFormControl && (\n <BubbleInput\n control={button}\n bubbles={!hasConsumerStoppedPropagationRef.current}\n name={name}\n value={value}\n checked={checked}\n required={required}\n disabled={disabled}\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n style={{ transform: 'translateX(-100%)' }}\n />\n )}\n </CheckboxProvider>\n );\n }\n);\n\nCheckbox.displayName = CHECKBOX_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * CheckboxIndicator\n * -----------------------------------------------------------------------------------------------*/\n\nconst INDICATOR_NAME = 'CheckboxIndicator';\n\ntype CheckboxIndicatorElement = React.ElementRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = Radix.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface CheckboxIndicatorProps extends PrimitiveSpanProps {\n /**\n * Used to force mounting when more control is needed. Useful when\n * controlling animation with React animation libraries.\n */\n forceMount?: true;\n}\n\nconst CheckboxIndicator = React.forwardRef<CheckboxIndicatorElement, CheckboxIndicatorProps>(\n (props: ScopedProps<CheckboxIndicatorProps>, forwardedRef) => {\n const { __scopeCheckbox, forceMount, ...indicatorProps } = props;\n const context = useCheckboxContext(INDICATOR_NAME, __scopeCheckbox);\n return (\n <Presence present={forceMount || isIndeterminate(context.state) || context.state === true}>\n <Primitive.span\n data-state={getState(context.state)}\n data-disabled={context.disabled ? '' : undefined}\n {...indicatorProps}\n ref={forwardedRef}\n style={{ pointerEvents: 'none', ...props.style }}\n />\n </Presence>\n );\n }\n);\n\nCheckboxIndicator.displayName = INDICATOR_NAME;\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype InputProps = Radix.ComponentPropsWithoutRef<'input'>;\ninterface BubbleInputProps extends Omit<InputProps, 'checked'> {\n checked: CheckedState;\n control: HTMLElement | null;\n bubbles: boolean;\n}\n\nconst BubbleInput = (props: BubbleInputProps) => {\n const { control, checked, bubbles = true, ...inputProps } = props;\n const ref = React.useRef<HTMLInputElement>(null);\n const prevChecked = usePrevious(checked);\n const controlSize = useSize(control);\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = ref.current!;\n const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked') as PropertyDescriptor;\n const setChecked = descriptor.set;\n\n if (prevChecked !== checked && setChecked) {\n const event = new Event('click', { bubbles });\n input.indeterminate = isIndeterminate(checked);\n setChecked.call(input, isIndeterminate(checked) ? false : checked);\n input.dispatchEvent(event);\n }\n }, [prevChecked, checked, bubbles]);\n\n return (\n <input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={isIndeterminate(checked) ? false : checked}\n {...inputProps}\n tabIndex={-1}\n ref={ref}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n }}\n />\n );\n};\n\nfunction isIndeterminate(checked?: CheckedState): checked is 'indeterminate' {\n return checked === 'indeterminate';\n}\n\nfunction getState(checked: CheckedState) {\n return isIndeterminate(checked) ? 'indeterminate' : checked ? 'checked' : 'unchecked';\n}\n\nconst Root = Checkbox;\nconst Indicator = CheckboxIndicator;\n\nexport {\n createCheckboxScope,\n //\n Checkbox,\n CheckboxIndicator,\n //\n Root,\n Indicator,\n};\nexport type { CheckboxProps, CheckboxIndicatorProps };\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,cAAuB;AACvB,kCAAgC;AAChC,6BAAmC;AACnC,yBAAqC;AACrC,4CAAqC;AACrC,kCAA4B;AAC5B,8BAAwB;AACxB,8BAAyB;AACzB,+BAA0B;AAmEpB;AA1DN,MAAM,gBAAgB;AAGtB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
|
-
import {useControllableState
|
|
7
|
-
import {usePrevious
|
|
8
|
-
import {useSize
|
|
9
|
-
import {Presence
|
|
10
|
-
import {Primitive
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
);
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
defaultProp: defaultChecked,
|
|
37
|
-
onChange: onCheckedChange
|
|
1
|
+
// packages/react/checkbox/src/Checkbox.tsx
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { useComposedRefs } from "@radix-ui/react-compose-refs";
|
|
4
|
+
import { createContextScope } from "@radix-ui/react-context";
|
|
5
|
+
import { composeEventHandlers } from "@radix-ui/primitive";
|
|
6
|
+
import { useControllableState } from "@radix-ui/react-use-controllable-state";
|
|
7
|
+
import { usePrevious } from "@radix-ui/react-use-previous";
|
|
8
|
+
import { useSize } from "@radix-ui/react-use-size";
|
|
9
|
+
import { Presence } from "@radix-ui/react-presence";
|
|
10
|
+
import { Primitive } from "@radix-ui/react-primitive";
|
|
11
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
12
|
+
var CHECKBOX_NAME = "Checkbox";
|
|
13
|
+
var [createCheckboxContext, createCheckboxScope] = createContextScope(CHECKBOX_NAME);
|
|
14
|
+
var [CheckboxProvider, useCheckboxContext] = createCheckboxContext(CHECKBOX_NAME);
|
|
15
|
+
var Checkbox = React.forwardRef(
|
|
16
|
+
(props, forwardedRef) => {
|
|
17
|
+
const {
|
|
18
|
+
__scopeCheckbox,
|
|
19
|
+
name,
|
|
20
|
+
checked: checkedProp,
|
|
21
|
+
defaultChecked,
|
|
22
|
+
required,
|
|
23
|
+
disabled,
|
|
24
|
+
value = "on",
|
|
25
|
+
onCheckedChange,
|
|
26
|
+
...checkboxProps
|
|
27
|
+
} = props;
|
|
28
|
+
const [button, setButton] = React.useState(null);
|
|
29
|
+
const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));
|
|
30
|
+
const hasConsumerStoppedPropagationRef = React.useRef(false);
|
|
31
|
+
const isFormControl = button ? Boolean(button.closest("form")) : true;
|
|
32
|
+
const [checked = false, setChecked] = useControllableState({
|
|
33
|
+
prop: checkedProp,
|
|
34
|
+
defaultProp: defaultChecked,
|
|
35
|
+
onChange: onCheckedChange
|
|
38
36
|
});
|
|
39
|
-
const initialCheckedStateRef =
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
button,
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
ref: composedRefs,
|
|
68
|
-
onKeyDown: $1bpvS$composeEventHandlers(props.onKeyDown, (event)=>{
|
|
69
|
-
// According to WAI ARIA, Checkboxes don't activate on enter keypress
|
|
70
|
-
if (event.key === 'Enter') event.preventDefault();
|
|
71
|
-
}),
|
|
72
|
-
onClick: $1bpvS$composeEventHandlers(props.onClick, (event)=>{
|
|
73
|
-
setChecked((prevChecked)=>$e698a72e93240346$var$isIndeterminate(prevChecked) ? true : !prevChecked
|
|
74
|
-
);
|
|
37
|
+
const initialCheckedStateRef = React.useRef(checked);
|
|
38
|
+
React.useEffect(() => {
|
|
39
|
+
const form = button?.form;
|
|
40
|
+
if (form) {
|
|
41
|
+
const reset = () => setChecked(initialCheckedStateRef.current);
|
|
42
|
+
form.addEventListener("reset", reset);
|
|
43
|
+
return () => form.removeEventListener("reset", reset);
|
|
44
|
+
}
|
|
45
|
+
}, [button, setChecked]);
|
|
46
|
+
return /* @__PURE__ */ jsxs(CheckboxProvider, { scope: __scopeCheckbox, state: checked, disabled, children: [
|
|
47
|
+
/* @__PURE__ */ jsx(
|
|
48
|
+
Primitive.button,
|
|
49
|
+
{
|
|
50
|
+
type: "button",
|
|
51
|
+
role: "checkbox",
|
|
52
|
+
"aria-checked": isIndeterminate(checked) ? "mixed" : checked,
|
|
53
|
+
"aria-required": required,
|
|
54
|
+
"data-state": getState(checked),
|
|
55
|
+
"data-disabled": disabled ? "" : void 0,
|
|
56
|
+
disabled,
|
|
57
|
+
value,
|
|
58
|
+
...checkboxProps,
|
|
59
|
+
ref: composedRefs,
|
|
60
|
+
onKeyDown: composeEventHandlers(props.onKeyDown, (event) => {
|
|
61
|
+
if (event.key === "Enter") event.preventDefault();
|
|
62
|
+
}),
|
|
63
|
+
onClick: composeEventHandlers(props.onClick, (event) => {
|
|
64
|
+
setChecked((prevChecked) => isIndeterminate(prevChecked) ? true : !prevChecked);
|
|
75
65
|
if (isFormControl) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
// form validation works and form events reflect checkbox updates.
|
|
79
|
-
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
|
|
66
|
+
hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();
|
|
67
|
+
if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();
|
|
80
68
|
}
|
|
81
|
-
|
|
82
|
-
})), isFormControl && /*#__PURE__*/ $1bpvS$createElement($e698a72e93240346$var$BubbleInput, {
|
|
83
|
-
control: button,
|
|
84
|
-
bubbles: !hasConsumerStoppedPropagationRef.current,
|
|
85
|
-
name: name,
|
|
86
|
-
value: value,
|
|
87
|
-
checked: checked,
|
|
88
|
-
required: required,
|
|
89
|
-
disabled: disabled // We transform because the input is absolutely positioned but we have
|
|
90
|
-
,
|
|
91
|
-
style: {
|
|
92
|
-
transform: 'translateX(-100%)'
|
|
93
|
-
}
|
|
94
|
-
}));
|
|
95
|
-
});
|
|
96
|
-
/*#__PURE__*/ Object.assign($e698a72e93240346$export$48513f6b9f8ce62d, {
|
|
97
|
-
displayName: $e698a72e93240346$var$CHECKBOX_NAME
|
|
98
|
-
});
|
|
99
|
-
/* -------------------------------------------------------------------------------------------------
|
|
100
|
-
* CheckboxIndicator
|
|
101
|
-
* -----------------------------------------------------------------------------------------------*/ const $e698a72e93240346$var$INDICATOR_NAME = 'CheckboxIndicator';
|
|
102
|
-
const $e698a72e93240346$export$59aad738f51d1c05 = /*#__PURE__*/ $1bpvS$forwardRef((props, forwardedRef)=>{
|
|
103
|
-
const { __scopeCheckbox: __scopeCheckbox , forceMount: forceMount , ...indicatorProps } = props;
|
|
104
|
-
const context = $e698a72e93240346$var$useCheckboxContext($e698a72e93240346$var$INDICATOR_NAME, __scopeCheckbox);
|
|
105
|
-
return /*#__PURE__*/ $1bpvS$createElement($1bpvS$Presence, {
|
|
106
|
-
present: forceMount || $e698a72e93240346$var$isIndeterminate(context.state) || context.state === true
|
|
107
|
-
}, /*#__PURE__*/ $1bpvS$createElement($1bpvS$Primitive.span, $1bpvS$babelruntimehelpersesmextends({
|
|
108
|
-
"data-state": $e698a72e93240346$var$getState(context.state),
|
|
109
|
-
"data-disabled": context.disabled ? '' : undefined
|
|
110
|
-
}, indicatorProps, {
|
|
111
|
-
ref: forwardedRef,
|
|
112
|
-
style: {
|
|
113
|
-
pointerEvents: 'none',
|
|
114
|
-
...props.style
|
|
69
|
+
})
|
|
115
70
|
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
const inputProto = window.HTMLInputElement.prototype;
|
|
129
|
-
const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked');
|
|
130
|
-
const setChecked = descriptor.set;
|
|
131
|
-
if (prevChecked !== checked && setChecked) {
|
|
132
|
-
const event = new Event('click', {
|
|
133
|
-
bubbles: bubbles
|
|
134
|
-
});
|
|
135
|
-
input.indeterminate = $e698a72e93240346$var$isIndeterminate(checked);
|
|
136
|
-
setChecked.call(input, $e698a72e93240346$var$isIndeterminate(checked) ? false : checked);
|
|
137
|
-
input.dispatchEvent(event);
|
|
71
|
+
),
|
|
72
|
+
isFormControl && /* @__PURE__ */ jsx(
|
|
73
|
+
BubbleInput,
|
|
74
|
+
{
|
|
75
|
+
control: button,
|
|
76
|
+
bubbles: !hasConsumerStoppedPropagationRef.current,
|
|
77
|
+
name,
|
|
78
|
+
value,
|
|
79
|
+
checked,
|
|
80
|
+
required,
|
|
81
|
+
disabled,
|
|
82
|
+
style: { transform: "translateX(-100%)" }
|
|
138
83
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
84
|
+
)
|
|
85
|
+
] });
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
Checkbox.displayName = CHECKBOX_NAME;
|
|
89
|
+
var INDICATOR_NAME = "CheckboxIndicator";
|
|
90
|
+
var CheckboxIndicator = React.forwardRef(
|
|
91
|
+
(props, forwardedRef) => {
|
|
92
|
+
const { __scopeCheckbox, forceMount, ...indicatorProps } = props;
|
|
93
|
+
const context = useCheckboxContext(INDICATOR_NAME, __scopeCheckbox);
|
|
94
|
+
return /* @__PURE__ */ jsx(Presence, { present: forceMount || isIndeterminate(context.state) || context.state === true, children: /* @__PURE__ */ jsx(
|
|
95
|
+
Primitive.span,
|
|
96
|
+
{
|
|
97
|
+
"data-state": getState(context.state),
|
|
98
|
+
"data-disabled": context.disabled ? "" : void 0,
|
|
99
|
+
...indicatorProps,
|
|
100
|
+
ref: forwardedRef,
|
|
101
|
+
style: { pointerEvents: "none", ...props.style }
|
|
102
|
+
}
|
|
103
|
+
) });
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
CheckboxIndicator.displayName = INDICATOR_NAME;
|
|
107
|
+
var BubbleInput = (props) => {
|
|
108
|
+
const { control, checked, bubbles = true, ...inputProps } = props;
|
|
109
|
+
const ref = React.useRef(null);
|
|
110
|
+
const prevChecked = usePrevious(checked);
|
|
111
|
+
const controlSize = useSize(control);
|
|
112
|
+
React.useEffect(() => {
|
|
113
|
+
const input = ref.current;
|
|
114
|
+
const inputProto = window.HTMLInputElement.prototype;
|
|
115
|
+
const descriptor = Object.getOwnPropertyDescriptor(inputProto, "checked");
|
|
116
|
+
const setChecked = descriptor.set;
|
|
117
|
+
if (prevChecked !== checked && setChecked) {
|
|
118
|
+
const event = new Event("click", { bubbles });
|
|
119
|
+
input.indeterminate = isIndeterminate(checked);
|
|
120
|
+
setChecked.call(input, isIndeterminate(checked) ? false : checked);
|
|
121
|
+
input.dispatchEvent(event);
|
|
122
|
+
}
|
|
123
|
+
}, [prevChecked, checked, bubbles]);
|
|
124
|
+
return /* @__PURE__ */ jsx(
|
|
125
|
+
"input",
|
|
126
|
+
{
|
|
127
|
+
type: "checkbox",
|
|
128
|
+
"aria-hidden": true,
|
|
129
|
+
defaultChecked: isIndeterminate(checked) ? false : checked,
|
|
130
|
+
...inputProps,
|
|
131
|
+
tabIndex: -1,
|
|
132
|
+
ref,
|
|
133
|
+
style: {
|
|
134
|
+
...props.style,
|
|
135
|
+
...controlSize,
|
|
136
|
+
position: "absolute",
|
|
137
|
+
pointerEvents: "none",
|
|
138
|
+
opacity: 0,
|
|
139
|
+
margin: 0
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
);
|
|
160
143
|
};
|
|
161
|
-
function
|
|
162
|
-
|
|
144
|
+
function isIndeterminate(checked) {
|
|
145
|
+
return checked === "indeterminate";
|
|
163
146
|
}
|
|
164
|
-
function
|
|
165
|
-
|
|
147
|
+
function getState(checked) {
|
|
148
|
+
return isIndeterminate(checked) ? "indeterminate" : checked ? "checked" : "unchecked";
|
|
166
149
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
150
|
+
var Root = Checkbox;
|
|
151
|
+
var Indicator = CheckboxIndicator;
|
|
152
|
+
export {
|
|
153
|
+
Checkbox,
|
|
154
|
+
CheckboxIndicator,
|
|
155
|
+
Indicator,
|
|
156
|
+
Root,
|
|
157
|
+
createCheckboxScope
|
|
158
|
+
};
|
|
174
159
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;A;;;;;;;;;;ACaA;;oGAEA,CAEA,MAAMc,mCAAa,GAAG,UAAtB,AAAA;AAGA,MAAM,CAACC,2CAAD,EAAwBf,yCAAxB,CAAA,GAA+CO,yBAAkB,CAACO,mCAAD,CAAvE,AAAA;AASA,MAAM,CAACE,sCAAD,EAAmBC,wCAAnB,CAAA,GACJF,2CAAqB,CAAuBD,mCAAvB,CADvB,AAAA;AAYA,MAAMb,yCAAQ,GAAA,aAAGI,CAAAA,iBAAA,CACf,CAACc,KAAD,EAAoCC,YAApC,GAAqD;IACnD,MAAM,E,iBACJC,eADI,CAAA,E,MAEJC,IAFI,CAAA,EAGJC,OAAO,EAAEC,WAHL,CAAA,E,gBAIJC,cAJI,CAAA,E,UAKJC,QALI,CAAA,E,UAMJC,QANI,CAAA,SAOJC,KAAK,GAAG,IAPJ,G,iBAQJC,eARI,CAAA,EASJ,GAAGC,aAAH,EATI,GAUFX,KAVJ,AAAM;IAWN,MAAM,CAACY,MAAD,EAASC,SAAT,CAAA,GAA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YC,CAAA;IAED,OAAA,aACE,CAAA,oBADF,CAAA,OAAA,EAAA,oCAAA,CAAA;QAEI,IAAI,EAAC,UADP;QAEE,aAAA,EAAA,IAFF;QAGE,cAAc,EAAErB,qCAAe,CAAC7B,OAAD,CAAf,GAA2B,KAA3B,GAAmCA,OAAnD;KAHF,EAIMmD,UAJN,EAAA;QAKE,QAAQ,EAAE,EALZ;QAME,GAAG,EAAEC,GANP;QAOE,KAAK,EAAE;YACL,GAAGxD,KAAK,CAACmD,KADJ;YAEL,GAAGM,WAFE;YAGLc,QAAQ,EAAE,UAHL;YAILrB,aAAa,EAAE,MAJV;YAKLsB,OAAO,EAAE,CALJ;YAMLC,MAAM,EAAE,CAARA;SANK;KAPT,CAAA,CADF,CACE;CAtBJ,AAuCC;AAED,SAASxC,qCAAT,CAAyB7B,OAAzB,EAA6E;IAC3E,OAAOA,OAAO,KAAK,eAAnB,CAAA;CACD;AAED,SAAS8B,8BAAT,CAAkB9B,OAAlB,EAAyC;IACvC,OAAO6B,qCAAe,CAAC7B,OAAD,CAAf,GAA2B,eAA3B,GAA6CA,OAAO,GAAG,SAAH,GAAe,WAA1E,CAAA;CACD;AAED,MAAMpB,yCAAI,GAAGF,yCAAb,AAAA;AACA,MAAMG,yCAAS,GAAGF,yCAAlB,AAAA;;AD3NA","sources":["packages/react/checkbox/src/index.ts","packages/react/checkbox/src/Checkbox.tsx"],"sourcesContent":["export {\n createCheckboxScope,\n //\n Checkbox,\n CheckboxIndicator,\n //\n Root,\n Indicator,\n} from './Checkbox';\nexport type { CheckboxProps, CheckboxIndicatorProps } from './Checkbox';\n","import * as React from 'react';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { composeEventHandlers } from '@radix-ui/primitive';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\nimport { usePrevious } from '@radix-ui/react-use-previous';\nimport { useSize } from '@radix-ui/react-use-size';\nimport { Presence } from '@radix-ui/react-presence';\nimport { Primitive } from '@radix-ui/react-primitive';\n\nimport type * as Radix from '@radix-ui/react-primitive';\nimport type { Scope } from '@radix-ui/react-context';\n\n/* -------------------------------------------------------------------------------------------------\n * Checkbox\n * -----------------------------------------------------------------------------------------------*/\n\nconst CHECKBOX_NAME = 'Checkbox';\n\ntype ScopedProps<P> = P & { __scopeCheckbox?: Scope };\nconst [createCheckboxContext, createCheckboxScope] = createContextScope(CHECKBOX_NAME);\n\ntype CheckedState = boolean | 'indeterminate';\n\ntype CheckboxContextValue = {\n state: CheckedState;\n disabled?: boolean;\n};\n\nconst [CheckboxProvider, useCheckboxContext] =\n createCheckboxContext<CheckboxContextValue>(CHECKBOX_NAME);\n\ntype CheckboxElement = React.ElementRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = Radix.ComponentPropsWithoutRef<typeof Primitive.button>;\ninterface CheckboxProps extends Omit<PrimitiveButtonProps, 'checked' | 'defaultChecked'> {\n checked?: CheckedState;\n defaultChecked?: CheckedState;\n required?: boolean;\n onCheckedChange?(checked: CheckedState): void;\n}\n\nconst Checkbox = React.forwardRef<CheckboxElement, CheckboxProps>(\n (props: ScopedProps<CheckboxProps>, forwardedRef) => {\n const {\n __scopeCheckbox,\n name,\n checked: checkedProp,\n defaultChecked,\n required,\n disabled,\n value = 'on',\n onCheckedChange,\n ...checkboxProps\n } = props;\n const [button, setButton] = React.useState<HTMLButtonElement | null>(null);\n const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));\n const hasConsumerStoppedPropagationRef = React.useRef(false);\n // We set this to true by default so that events bubble to forms without JS (SSR)\n const isFormControl = button ? Boolean(button.closest('form')) : true;\n const [checked = false, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked,\n onChange: onCheckedChange,\n });\n const initialCheckedStateRef = React.useRef(checked);\n React.useEffect(() => {\n const form = button?.form;\n if (form) {\n const reset = () => setChecked(initialCheckedStateRef.current);\n form.addEventListener('reset', reset);\n return () => form.removeEventListener('reset', reset);\n }\n }, [button, setChecked]);\n\n return (\n <CheckboxProvider scope={__scopeCheckbox} state={checked} disabled={disabled}>\n <Primitive.button\n type=\"button\"\n role=\"checkbox\"\n aria-checked={isIndeterminate(checked) ? 'mixed' : checked}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n value={value}\n {...checkboxProps}\n ref={composedRefs}\n onKeyDown={composeEventHandlers(props.onKeyDown, (event) => {\n // According to WAI ARIA, Checkboxes don't activate on enter keypress\n if (event.key === 'Enter') event.preventDefault();\n })}\n onClick={composeEventHandlers(props.onClick, (event) => {\n setChecked((prevChecked) => (isIndeterminate(prevChecked) ? true : !prevChecked));\n if (isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();\n // if checkbox is in a form, stop propagation from the button so that we only propagate\n // one click event (from the input). We propagate changes from an input so that native\n // form validation works and form events reflect checkbox updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();\n }\n })}\n />\n {isFormControl && (\n <BubbleInput\n control={button}\n bubbles={!hasConsumerStoppedPropagationRef.current}\n name={name}\n value={value}\n checked={checked}\n required={required}\n disabled={disabled}\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n style={{ transform: 'translateX(-100%)' }}\n />\n )}\n </CheckboxProvider>\n );\n }\n);\n\nCheckbox.displayName = CHECKBOX_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * CheckboxIndicator\n * -----------------------------------------------------------------------------------------------*/\n\nconst INDICATOR_NAME = 'CheckboxIndicator';\n\ntype CheckboxIndicatorElement = React.ElementRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = Radix.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface CheckboxIndicatorProps extends PrimitiveSpanProps {\n /**\n * Used to force mounting when more control is needed. Useful when\n * controlling animation with React animation libraries.\n */\n forceMount?: true;\n}\n\nconst CheckboxIndicator = React.forwardRef<CheckboxIndicatorElement, CheckboxIndicatorProps>(\n (props: ScopedProps<CheckboxIndicatorProps>, forwardedRef) => {\n const { __scopeCheckbox, forceMount, ...indicatorProps } = props;\n const context = useCheckboxContext(INDICATOR_NAME, __scopeCheckbox);\n return (\n <Presence present={forceMount || isIndeterminate(context.state) || context.state === true}>\n <Primitive.span\n data-state={getState(context.state)}\n data-disabled={context.disabled ? '' : undefined}\n {...indicatorProps}\n ref={forwardedRef}\n style={{ pointerEvents: 'none', ...props.style }}\n />\n </Presence>\n );\n }\n);\n\nCheckboxIndicator.displayName = INDICATOR_NAME;\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype InputProps = Radix.ComponentPropsWithoutRef<'input'>;\ninterface BubbleInputProps extends Omit<InputProps, 'checked'> {\n checked: CheckedState;\n control: HTMLElement | null;\n bubbles: boolean;\n}\n\nconst BubbleInput = (props: BubbleInputProps) => {\n const { control, checked, bubbles = true, ...inputProps } = props;\n const ref = React.useRef<HTMLInputElement>(null);\n const prevChecked = usePrevious(checked);\n const controlSize = useSize(control);\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = ref.current!;\n const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked') as PropertyDescriptor;\n const setChecked = descriptor.set;\n\n if (prevChecked !== checked && setChecked) {\n const event = new Event('click', { bubbles });\n input.indeterminate = isIndeterminate(checked);\n setChecked.call(input, isIndeterminate(checked) ? false : checked);\n input.dispatchEvent(event);\n }\n }, [prevChecked, checked, bubbles]);\n\n return (\n <input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={isIndeterminate(checked) ? false : checked}\n {...inputProps}\n tabIndex={-1}\n ref={ref}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n }}\n />\n );\n};\n\nfunction isIndeterminate(checked?: CheckedState): checked is 'indeterminate' {\n return checked === 'indeterminate';\n}\n\nfunction getState(checked: CheckedState) {\n return isIndeterminate(checked) ? 'indeterminate' : checked ? 'checked' : 'unchecked';\n}\n\nconst Root = Checkbox;\nconst Indicator = CheckboxIndicator;\n\nexport {\n createCheckboxScope,\n //\n Checkbox,\n CheckboxIndicator,\n //\n Root,\n Indicator,\n};\nexport type { CheckboxProps, CheckboxIndicatorProps };\n"],"names":["createCheckboxScope","Checkbox","CheckboxIndicator","Root","Indicator","React","useComposedRefs","createContextScope","composeEventHandlers","useControllableState","usePrevious","useSize","Presence","Primitive","CHECKBOX_NAME","createCheckboxContext","CheckboxProvider","useCheckboxContext","forwardRef","props","forwardedRef","__scopeCheckbox","name","checked","checkedProp","defaultChecked","required","disabled","value","onCheckedChange","checkboxProps","button","setButton","useState","composedRefs","node","hasConsumerStoppedPropagationRef","useRef","isFormControl","Boolean","closest","setChecked","prop","defaultProp","onChange","initialCheckedStateRef","useEffect","form","reset","current","addEventListener","removeEventListener","isIndeterminate","getState","undefined","onKeyDown","event","key","preventDefault","onClick","prevChecked","isPropagationStopped","stopPropagation","transform","INDICATOR_NAME","forceMount","indicatorProps","context","state","pointerEvents","style","BubbleInput","control","bubbles","inputProps","ref","controlSize","input","inputProto","window","HTMLInputElement","prototype","descriptor","Object","getOwnPropertyDescriptor","set","Event","indeterminate","call","dispatchEvent","position","opacity","margin"],"version":3,"file":"index.mjs.map"}
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/Checkbox.tsx"],
|
|
4
|
+
"sourcesContent": ["import * as React from 'react';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { createContextScope } from '@radix-ui/react-context';\nimport { composeEventHandlers } from '@radix-ui/primitive';\nimport { useControllableState } from '@radix-ui/react-use-controllable-state';\nimport { usePrevious } from '@radix-ui/react-use-previous';\nimport { useSize } from '@radix-ui/react-use-size';\nimport { Presence } from '@radix-ui/react-presence';\nimport { Primitive } from '@radix-ui/react-primitive';\n\nimport type * as Radix from '@radix-ui/react-primitive';\nimport type { Scope } from '@radix-ui/react-context';\n\n/* -------------------------------------------------------------------------------------------------\n * Checkbox\n * -----------------------------------------------------------------------------------------------*/\n\nconst CHECKBOX_NAME = 'Checkbox';\n\ntype ScopedProps<P> = P & { __scopeCheckbox?: Scope };\nconst [createCheckboxContext, createCheckboxScope] = createContextScope(CHECKBOX_NAME);\n\ntype CheckedState = boolean | 'indeterminate';\n\ntype CheckboxContextValue = {\n state: CheckedState;\n disabled?: boolean;\n};\n\nconst [CheckboxProvider, useCheckboxContext] =\n createCheckboxContext<CheckboxContextValue>(CHECKBOX_NAME);\n\ntype CheckboxElement = React.ElementRef<typeof Primitive.button>;\ntype PrimitiveButtonProps = Radix.ComponentPropsWithoutRef<typeof Primitive.button>;\ninterface CheckboxProps extends Omit<PrimitiveButtonProps, 'checked' | 'defaultChecked'> {\n checked?: CheckedState;\n defaultChecked?: CheckedState;\n required?: boolean;\n onCheckedChange?(checked: CheckedState): void;\n}\n\nconst Checkbox = React.forwardRef<CheckboxElement, CheckboxProps>(\n (props: ScopedProps<CheckboxProps>, forwardedRef) => {\n const {\n __scopeCheckbox,\n name,\n checked: checkedProp,\n defaultChecked,\n required,\n disabled,\n value = 'on',\n onCheckedChange,\n ...checkboxProps\n } = props;\n const [button, setButton] = React.useState<HTMLButtonElement | null>(null);\n const composedRefs = useComposedRefs(forwardedRef, (node) => setButton(node));\n const hasConsumerStoppedPropagationRef = React.useRef(false);\n // We set this to true by default so that events bubble to forms without JS (SSR)\n const isFormControl = button ? Boolean(button.closest('form')) : true;\n const [checked = false, setChecked] = useControllableState({\n prop: checkedProp,\n defaultProp: defaultChecked,\n onChange: onCheckedChange,\n });\n const initialCheckedStateRef = React.useRef(checked);\n React.useEffect(() => {\n const form = button?.form;\n if (form) {\n const reset = () => setChecked(initialCheckedStateRef.current);\n form.addEventListener('reset', reset);\n return () => form.removeEventListener('reset', reset);\n }\n }, [button, setChecked]);\n\n return (\n <CheckboxProvider scope={__scopeCheckbox} state={checked} disabled={disabled}>\n <Primitive.button\n type=\"button\"\n role=\"checkbox\"\n aria-checked={isIndeterminate(checked) ? 'mixed' : checked}\n aria-required={required}\n data-state={getState(checked)}\n data-disabled={disabled ? '' : undefined}\n disabled={disabled}\n value={value}\n {...checkboxProps}\n ref={composedRefs}\n onKeyDown={composeEventHandlers(props.onKeyDown, (event) => {\n // According to WAI ARIA, Checkboxes don't activate on enter keypress\n if (event.key === 'Enter') event.preventDefault();\n })}\n onClick={composeEventHandlers(props.onClick, (event) => {\n setChecked((prevChecked) => (isIndeterminate(prevChecked) ? true : !prevChecked));\n if (isFormControl) {\n hasConsumerStoppedPropagationRef.current = event.isPropagationStopped();\n // if checkbox is in a form, stop propagation from the button so that we only propagate\n // one click event (from the input). We propagate changes from an input so that native\n // form validation works and form events reflect checkbox updates.\n if (!hasConsumerStoppedPropagationRef.current) event.stopPropagation();\n }\n })}\n />\n {isFormControl && (\n <BubbleInput\n control={button}\n bubbles={!hasConsumerStoppedPropagationRef.current}\n name={name}\n value={value}\n checked={checked}\n required={required}\n disabled={disabled}\n // We transform because the input is absolutely positioned but we have\n // rendered it **after** the button. This pulls it back to sit on top\n // of the button.\n style={{ transform: 'translateX(-100%)' }}\n />\n )}\n </CheckboxProvider>\n );\n }\n);\n\nCheckbox.displayName = CHECKBOX_NAME;\n\n/* -------------------------------------------------------------------------------------------------\n * CheckboxIndicator\n * -----------------------------------------------------------------------------------------------*/\n\nconst INDICATOR_NAME = 'CheckboxIndicator';\n\ntype CheckboxIndicatorElement = React.ElementRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = Radix.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface CheckboxIndicatorProps extends PrimitiveSpanProps {\n /**\n * Used to force mounting when more control is needed. Useful when\n * controlling animation with React animation libraries.\n */\n forceMount?: true;\n}\n\nconst CheckboxIndicator = React.forwardRef<CheckboxIndicatorElement, CheckboxIndicatorProps>(\n (props: ScopedProps<CheckboxIndicatorProps>, forwardedRef) => {\n const { __scopeCheckbox, forceMount, ...indicatorProps } = props;\n const context = useCheckboxContext(INDICATOR_NAME, __scopeCheckbox);\n return (\n <Presence present={forceMount || isIndeterminate(context.state) || context.state === true}>\n <Primitive.span\n data-state={getState(context.state)}\n data-disabled={context.disabled ? '' : undefined}\n {...indicatorProps}\n ref={forwardedRef}\n style={{ pointerEvents: 'none', ...props.style }}\n />\n </Presence>\n );\n }\n);\n\nCheckboxIndicator.displayName = INDICATOR_NAME;\n\n/* ---------------------------------------------------------------------------------------------- */\n\ntype InputProps = Radix.ComponentPropsWithoutRef<'input'>;\ninterface BubbleInputProps extends Omit<InputProps, 'checked'> {\n checked: CheckedState;\n control: HTMLElement | null;\n bubbles: boolean;\n}\n\nconst BubbleInput = (props: BubbleInputProps) => {\n const { control, checked, bubbles = true, ...inputProps } = props;\n const ref = React.useRef<HTMLInputElement>(null);\n const prevChecked = usePrevious(checked);\n const controlSize = useSize(control);\n\n // Bubble checked change to parents (e.g form change event)\n React.useEffect(() => {\n const input = ref.current!;\n const inputProto = window.HTMLInputElement.prototype;\n const descriptor = Object.getOwnPropertyDescriptor(inputProto, 'checked') as PropertyDescriptor;\n const setChecked = descriptor.set;\n\n if (prevChecked !== checked && setChecked) {\n const event = new Event('click', { bubbles });\n input.indeterminate = isIndeterminate(checked);\n setChecked.call(input, isIndeterminate(checked) ? false : checked);\n input.dispatchEvent(event);\n }\n }, [prevChecked, checked, bubbles]);\n\n return (\n <input\n type=\"checkbox\"\n aria-hidden\n defaultChecked={isIndeterminate(checked) ? false : checked}\n {...inputProps}\n tabIndex={-1}\n ref={ref}\n style={{\n ...props.style,\n ...controlSize,\n position: 'absolute',\n pointerEvents: 'none',\n opacity: 0,\n margin: 0,\n }}\n />\n );\n};\n\nfunction isIndeterminate(checked?: CheckedState): checked is 'indeterminate' {\n return checked === 'indeterminate';\n}\n\nfunction getState(checked: CheckedState) {\n return isIndeterminate(checked) ? 'indeterminate' : checked ? 'checked' : 'unchecked';\n}\n\nconst Root = Checkbox;\nconst Indicator = CheckboxIndicator;\n\nexport {\n createCheckboxSc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,SAAS,SAAuB;AACvC,SAAO,gBAAgB,OAAO,IAAI,kBAAkB,UAAU,YAAY;AAC5E;AAEA,IAAM,OAAO;AACb,IAAM,YAAY;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@radix-ui/react-checkbox",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.1.0-rc.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -28,15 +28,14 @@
|
|
|
28
28
|
"version": "yarn version"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@
|
|
32
|
-
"@radix-ui/
|
|
33
|
-
"@radix-ui/react-
|
|
34
|
-
"@radix-ui/react-
|
|
35
|
-
"@radix-ui/react-
|
|
36
|
-
"@radix-ui/react-
|
|
37
|
-
"@radix-ui/react-use-
|
|
38
|
-
"@radix-ui/react-use-
|
|
39
|
-
"@radix-ui/react-use-size": "1.0.1"
|
|
31
|
+
"@radix-ui/primitive": "1.1.0-rc.1",
|
|
32
|
+
"@radix-ui/react-compose-refs": "1.1.0-rc.1",
|
|
33
|
+
"@radix-ui/react-context": "1.1.0-rc.1",
|
|
34
|
+
"@radix-ui/react-presence": "1.1.0-rc.1",
|
|
35
|
+
"@radix-ui/react-primitive": "1.1.0-rc.1",
|
|
36
|
+
"@radix-ui/react-use-controllable-state": "1.1.0-rc.1",
|
|
37
|
+
"@radix-ui/react-use-previous": "1.1.0-rc.1",
|
|
38
|
+
"@radix-ui/react-use-size": "1.1.0-rc.1"
|
|
40
39
|
},
|
|
41
40
|
"peerDependencies": {
|
|
42
41
|
"@types/react": "*",
|
|
@@ -59,5 +58,6 @@
|
|
|
59
58
|
},
|
|
60
59
|
"bugs": {
|
|
61
60
|
"url": "https://github.com/radix-ui/primitives/issues"
|
|
62
|
-
}
|
|
61
|
+
},
|
|
62
|
+
"stableVersion": "1.0.4"
|
|
63
63
|
}
|
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"mappings":";;;AAoBA,OAAA,0FAAsF,CAAC;AAEvF,oBAAoB,OAAO,GAAG,eAAe,CAAC;AAW9C,4BAA4B,MAAM,wBAAwB,CAAC,OAAO,UAAU,MAAM,CAAC,CAAC;AACpF,8BAAwB,SAAQ,IAAI,CAAC,oBAAoB,EAAE,SAAS,GAAG,gBAAgB,CAAC;IACtF,OAAO,CAAC,EAAE,YAAY,CAAC;IACvB,cAAc,CAAC,EAAE,YAAY,CAAC;IAC9B,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,eAAe,CAAC,CAAC,OAAO,EAAE,YAAY,GAAG,IAAI,CAAC;CAC/C;AAED,OAAA,MAAM,iGA+EL,CAAC;AAWF,0BAA0B,MAAM,wBAAwB,CAAC,OAAO,UAAU,IAAI,CAAC,CAAC;AAChF,uCAAiC,SAAQ,kBAAkB;IACzD;;;OAGG;IACH,UAAU,CAAC,EAAE,IAAI,CAAC;CACnB;AAED,OAAA,MAAM,iHAgBL,CAAC;AA8DF,OAAA,MAAM,6FAAe,CAAC;AACtB,OAAA,MAAM,yGAA6B,CAAC","sources":["packages/react/checkbox/src/packages/react/checkbox/src/Checkbox.tsx","packages/react/checkbox/src/packages/react/checkbox/src/index.ts","packages/react/checkbox/src/index.ts"],"sourcesContent":[null,null,"export {\n createCheckboxScope,\n //\n Checkbox,\n CheckboxIndicator,\n //\n Root,\n Indicator,\n} from './Checkbox';\nexport type { CheckboxProps, CheckboxIndicatorProps } from './Checkbox';\n"],"names":[],"version":3,"file":"index.d.ts.map"}
|