@radix-ui/react-label 0.1.6-rc.2 → 0.1.6-rc.5
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.ts.map +1 -1
- package/dist/index.js +134 -1
- package/dist/index.js.map +1 -1
- package/dist/index.module.js +115 -1
- package/dist/index.module.js.map +1 -1
- package/package.json +5 -5
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":";;;AAqBA,0BAA0B,MAAM,wBAAwB,CAAC,OAAO,UAAU,IAAI,CAAC,CAAC;AAChF,2BAAqB,SAAQ,kBAAkB;IAC7C,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED,OAAA,MAAM,yFAgEJ,CAAC;AAMH,OAAA,MAAM,iFASL,CAAC;AAEF,OAAA,MAAM,wFAAY,CAAC","sources":["packages/react/label/src/packages/react/label/src/Label.tsx","packages/react/label/src/packages/react/label/src/index.ts","packages/react/label/src/index.ts"],"sourcesContent":[null,null,"export * from './Label';\n"],"names":[],"version":3,"file":"index.d.ts.map"}
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,135 @@
|
|
|
1
|
-
var
|
|
1
|
+
var $RyX6d$babelruntimehelpersextends = require("@babel/runtime/helpers/extends");
|
|
2
|
+
var $RyX6d$react = require("react");
|
|
3
|
+
var $RyX6d$radixuireactcontext = require("@radix-ui/react-context");
|
|
4
|
+
var $RyX6d$radixuireactcomposerefs = require("@radix-ui/react-compose-refs");
|
|
5
|
+
var $RyX6d$radixuireactprimitive = require("@radix-ui/react-primitive");
|
|
6
|
+
var $RyX6d$radixuireactid = require("@radix-ui/react-id");
|
|
7
|
+
|
|
8
|
+
function $parcel$exportWildcard(dest, source) {
|
|
9
|
+
Object.keys(source).forEach(function(key) {
|
|
10
|
+
if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
Object.defineProperty(dest, key, {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function get() {
|
|
17
|
+
return source[key];
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return dest;
|
|
23
|
+
}
|
|
24
|
+
function $parcel$interopDefault(a) {
|
|
25
|
+
return a && a.__esModule ? a.default : a;
|
|
26
|
+
}
|
|
27
|
+
function $parcel$export(e, n, v, s) {
|
|
28
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
29
|
+
}
|
|
30
|
+
var $2583d9c3eccdd4e2$exports = {};
|
|
31
|
+
|
|
32
|
+
$parcel$export($2583d9c3eccdd4e2$exports, "Label", () => $2583d9c3eccdd4e2$export$b04be29aa201d4f5);
|
|
33
|
+
$parcel$export($2583d9c3eccdd4e2$exports, "useLabelContext", () => $2583d9c3eccdd4e2$export$feddbbf47baabdb9);
|
|
34
|
+
$parcel$export($2583d9c3eccdd4e2$exports, "Root", () => $2583d9c3eccdd4e2$export$be92b6f5f03c0fe9);
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
/* -------------------------------------------------------------------------------------------------
|
|
42
|
+
* Label
|
|
43
|
+
* -----------------------------------------------------------------------------------------------*/ const $2583d9c3eccdd4e2$var$NAME = 'Label';
|
|
44
|
+
const [$2583d9c3eccdd4e2$var$LabelProvider, $2583d9c3eccdd4e2$var$useLabelContextImpl] = $RyX6d$radixuireactcontext.createContext($2583d9c3eccdd4e2$var$NAME, {
|
|
45
|
+
id: undefined,
|
|
46
|
+
controlRef: {
|
|
47
|
+
current: null
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
const $2583d9c3eccdd4e2$export$b04be29aa201d4f5 = /*#__PURE__*/ $RyX6d$react.forwardRef((props, forwardedRef)=>{
|
|
51
|
+
const { htmlFor: htmlFor , id: idProp , ...labelProps } = props;
|
|
52
|
+
const controlRef = $RyX6d$react.useRef(null);
|
|
53
|
+
const ref = $RyX6d$react.useRef(null);
|
|
54
|
+
const composedRefs = $RyX6d$radixuireactcomposerefs.useComposedRefs(forwardedRef, ref);
|
|
55
|
+
const id = $RyX6d$radixuireactid.useId(idProp);
|
|
56
|
+
$RyX6d$react.useEffect(()=>{
|
|
57
|
+
if (htmlFor) {
|
|
58
|
+
const element = document.getElementById(htmlFor);
|
|
59
|
+
const label = ref.current;
|
|
60
|
+
if (label && element) {
|
|
61
|
+
const getAriaLabel = ()=>element.getAttribute('aria-labelledby')
|
|
62
|
+
;
|
|
63
|
+
const ariaLabelledBy1 = [
|
|
64
|
+
id,
|
|
65
|
+
getAriaLabel()
|
|
66
|
+
].filter(Boolean).join(' ');
|
|
67
|
+
element.setAttribute('aria-labelledby', ariaLabelledBy1);
|
|
68
|
+
controlRef.current = element;
|
|
69
|
+
return ()=>{
|
|
70
|
+
var _getAriaLabel;
|
|
71
|
+
/**
|
|
72
|
+
* We get the latest attribute value because at the time that this cleanup fires,
|
|
73
|
+
* the values from the closure may have changed.
|
|
74
|
+
*/ const ariaLabelledBy = (_getAriaLabel = getAriaLabel()) === null || _getAriaLabel === void 0 ? void 0 : _getAriaLabel.replace(id, '');
|
|
75
|
+
if (ariaLabelledBy === '') element.removeAttribute('aria-labelledby');
|
|
76
|
+
else if (ariaLabelledBy) element.setAttribute('aria-labelledby', ariaLabelledBy);
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}, [
|
|
81
|
+
id,
|
|
82
|
+
htmlFor
|
|
83
|
+
]);
|
|
84
|
+
return /*#__PURE__*/ $RyX6d$react.createElement($2583d9c3eccdd4e2$var$LabelProvider, {
|
|
85
|
+
id: id,
|
|
86
|
+
controlRef: controlRef
|
|
87
|
+
}, /*#__PURE__*/ $RyX6d$react.createElement($RyX6d$radixuireactprimitive.Primitive.span, ($parcel$interopDefault($RyX6d$babelruntimehelpersextends))({
|
|
88
|
+
role: "label",
|
|
89
|
+
id: id
|
|
90
|
+
}, labelProps, {
|
|
91
|
+
ref: composedRefs,
|
|
92
|
+
onMouseDown: (event)=>{
|
|
93
|
+
var _props$onMouseDown;
|
|
94
|
+
(_props$onMouseDown = props.onMouseDown) === null || _props$onMouseDown === void 0 || _props$onMouseDown.call(props, event); // prevent text selection when double clicking label
|
|
95
|
+
if (!event.defaultPrevented && event.detail > 1) event.preventDefault();
|
|
96
|
+
},
|
|
97
|
+
onClick: (event)=>{
|
|
98
|
+
var _props$onClick;
|
|
99
|
+
(_props$onClick = props.onClick) === null || _props$onClick === void 0 || _props$onClick.call(props, event);
|
|
100
|
+
if (!controlRef.current || event.defaultPrevented) return;
|
|
101
|
+
const isClickingControl = controlRef.current.contains(event.target); // Ensure event was generated by a user action
|
|
102
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted
|
|
103
|
+
const isUserClick = event.isTrusted === true;
|
|
104
|
+
/**
|
|
105
|
+
* When a label is wrapped around the control it labels, we trigger the appropriate events
|
|
106
|
+
* on the control when the label is clicked. We do nothing if the user is already clicking the
|
|
107
|
+
* control inside the label.
|
|
108
|
+
*/ if (!isClickingControl && isUserClick) {
|
|
109
|
+
controlRef.current.click();
|
|
110
|
+
controlRef.current.focus();
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
})));
|
|
114
|
+
});
|
|
115
|
+
/*#__PURE__*/ Object.assign($2583d9c3eccdd4e2$export$b04be29aa201d4f5, {
|
|
116
|
+
displayName: $2583d9c3eccdd4e2$var$NAME
|
|
117
|
+
});
|
|
118
|
+
/* -----------------------------------------------------------------------------------------------*/ const $2583d9c3eccdd4e2$export$feddbbf47baabdb9 = (element)=>{
|
|
119
|
+
const context = $2583d9c3eccdd4e2$var$useLabelContextImpl('LabelConsumer');
|
|
120
|
+
const { controlRef: controlRef } = context;
|
|
121
|
+
$RyX6d$react.useEffect(()=>{
|
|
122
|
+
if (element) controlRef.current = element;
|
|
123
|
+
}, [
|
|
124
|
+
element,
|
|
125
|
+
controlRef
|
|
126
|
+
]);
|
|
127
|
+
return context.id;
|
|
128
|
+
};
|
|
129
|
+
const $2583d9c3eccdd4e2$export$be92b6f5f03c0fe9 = $2583d9c3eccdd4e2$export$b04be29aa201d4f5;
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
$parcel$exportWildcard(module.exports, $2583d9c3eccdd4e2$exports);
|
|
133
|
+
|
|
134
|
+
|
|
2
135
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;A;;;;;;ACQA;;oGAEA,CAEA,MAAMK,0BAAI,GAAG,OAAb,AAAA;AAGA,MAAM,CAACC,mCAAD,EAAgBC,yCAAhB,CAAA,GAAuCN,wCAAa,CAAoBI,0BAApB,EAA0B;IAClFG,EAAE,EAAEC,SAD8E;IAElFC,UAAU,EAAE;QAAEC,OAAO,EAAE,IAATA;KAAF;CAF4C,CAA1D,AAAoF;AAWpF,MAAMC,yCAAK,GAAA,aAAGZ,CAAAA,uBAAA,CAA2C,CAACc,KAAD,EAAQC,YAAR,GAAyB;IAChF,MAAM,E,SAAEC,OAAF,CAAA,EAAWR,EAAE,EAAES,MAAf,CAAA,EAAuB,GAAGC,UAAH,EAAvB,GAAyCJ,KAA/C,AAAM;IACN,MAAMJ,UAAU,GAAGV,mBAAA,CAAiC,IAAjC,CAAnB,AAAA;IACA,MAAMoB,GAAG,GAAGpB,mBAAA,CAA8B,IAA9B,CAAZ,AAAA;IACA,MAAMqB,YAAY,GAAGnB,8CAAe,CAACa,YAAD,EAAeK,GAAf,CAApC,AAAA;IACA,MAAMZ,EAAE,GAAGJ,2BAAK,CAACa,MAAD,CAAhB,AAAA;IAEAjB,sBAAA,CAAgB,IAAM;QACpB,IAAIgB,OAAJ,EAAa;YACX,MAAMO,OAAO,GAAGC,QAA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A,CAAA,CAAA;AAEA,oGAAA,CAEA,MAAMuC,yCAAe,GAAI1B,CAAAA,OAAD,GAAkC;IACxD,MAAM2B,OAAO,GAAG3C,yCAAmB,CAAC,eAAD,CAAnC,AAAA;IACA,MAAM,E,YAAEG,UAAAA,CAAAA,EAAF,GAAiBwC,OAAvB,AAAM;IAENlD,sBAAA,CAAgB,IAAM;QACpB,IAAIuB,OAAJ,EAAab,UAAU,CAACC,OAAX,GAAqBY,OAArB,CAAb;KADF,EAEG;QAACA,OAAD;QAAUb,UAAV;KAFH,CAEC,CAAA;IAED,OAAOwC,OAAO,CAAC1C,EAAf,CAAA;CARF,AASC;AAED,MAAM2C,yCAAI,GAAGvC,yCAAb,AAAA;;AD3GA","sources":["packages/react/label/src/index.ts","packages/react/label/src/Label.tsx"],"sourcesContent":["export * from './Label';\n","import * as React from 'react';\nimport { createContext } from '@radix-ui/react-context';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { Primitive } from '@radix-ui/react-primitive';\nimport { useId } from '@radix-ui/react-id';\n\nimport type * as Radix from '@radix-ui/react-primitive';\n\n/* -------------------------------------------------------------------------------------------------\n * Label\n * -----------------------------------------------------------------------------------------------*/\n\nconst NAME = 'Label';\n\ntype LabelContextValue = { id?: string; controlRef: React.MutableRefObject<HTMLElement | null> };\nconst [LabelProvider, useLabelContextImpl] = createContext<LabelContextValue>(NAME, {\n id: undefined,\n controlRef: { current: null },\n});\n\ntype LabelElement = React.ElementRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = Radix.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface LabelProps extends PrimitiveSpanProps {\n htmlFor?: string;\n}\n\nconst Label = React.forwardRef<LabelElement, LabelProps>((props, forwardedRef) => {\n const { htmlFor, id: idProp, ...labelProps } = props;\n const controlRef = React.useRef<HTMLElement | null>(null);\n const ref = React.useRef<HTMLSpanElement>(null);\n const composedRefs = useComposedRefs(forwardedRef, ref);\n const id = useId(idProp);\n\n React.useEffect(() => {\n if (htmlFor) {\n const element = document.getElementById(htmlFor);\n const label = ref.current;\n if (label && element) {\n const getAriaLabel = () => element.getAttribute('aria-labelledby');\n const ariaLabelledBy = [id, getAriaLabel()].filter(Boolean).join(' ');\n element.setAttribute('aria-labelledby', ariaLabelledBy);\n controlRef.current = element;\n return () => {\n /**\n * We get the latest attribute value because at the time that this cleanup fires,\n * the values from the closure may have changed.\n */\n const ariaLabelledBy = getAriaLabel()?.replace(id, '');\n if (ariaLabelledBy === '') {\n element.removeAttribute('aria-labelledby');\n } else if (ariaLabelledBy) {\n element.setAttribute('aria-labelledby', ariaLabelledBy);\n }\n };\n }\n }\n }, [id, htmlFor]);\n\n return (\n <LabelProvider id={id} controlRef={controlRef}>\n <Primitive.span\n role=\"label\"\n id={id}\n {...labelProps}\n ref={composedRefs}\n onMouseDown={(event) => {\n props.onMouseDown?.(event);\n // prevent text selection when double clicking label\n if (!event.defaultPrevented && event.detail > 1) event.preventDefault();\n }}\n onClick={(event) => {\n props.onClick?.(event);\n if (!controlRef.current || event.defaultPrevented) return;\n const isClickingControl = controlRef.current.contains(event.target as Node);\n // Ensure event was generated by a user action\n // https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted\n const isUserClick = event.isTrusted === true;\n /**\n * When a label is wrapped around the control it labels, we trigger the appropriate events\n * on the control when the label is clicked. We do nothing if the user is already clicking the\n * control inside the label.\n */\n if (!isClickingControl && isUserClick) {\n controlRef.current.click();\n controlRef.current.focus();\n }\n }}\n />\n </LabelProvider>\n );\n});\n\nLabel.displayName = NAME;\n\n/* -----------------------------------------------------------------------------------------------*/\n\nconst useLabelContext = (element?: HTMLElement | null) => {\n const context = useLabelContextImpl('LabelConsumer');\n const { controlRef } = context;\n\n React.useEffect(() => {\n if (element) controlRef.current = element;\n }, [element, controlRef]);\n\n return context.id;\n};\n\nconst Root = Label;\n\nexport {\n Label,\n //\n Root,\n //\n useLabelContext,\n};\nexport type { LabelProps };\n"],"names":["React","createContext","useComposedRefs","Primitive","useId","NAME","LabelProvider","useLabelContextImpl","id","undefined","controlRef","current","Label","forwardRef","props","forwardedRef","htmlFor","idProp","labelProps","useRef","ref","composedRefs","useEffect","element","document","getElementById","label","getAriaLabel","getAttribute","ariaLabelledBy","filter","Boolean","join","setAttribute","replace","removeAttribute","event","onMouseDown","defaultPrevented","detail","preventDefault","onClick","isClickingControl","contains","target","isUserClick","isTrusted","click","focus","useLabelContext","context","Root"],"version":3,"file":"index.js.map"}
|
package/dist/index.module.js
CHANGED
|
@@ -1,2 +1,116 @@
|
|
|
1
|
-
import
|
|
1
|
+
import $7V4JZ$babelruntimehelpersesmextends from "@babel/runtime/helpers/esm/extends";
|
|
2
|
+
import {forwardRef as $7V4JZ$forwardRef, useRef as $7V4JZ$useRef, useEffect as $7V4JZ$useEffect, createElement as $7V4JZ$createElement} from "react";
|
|
3
|
+
import {createContext as $7V4JZ$createContext} from "@radix-ui/react-context";
|
|
4
|
+
import {useComposedRefs as $7V4JZ$useComposedRefs} from "@radix-ui/react-compose-refs";
|
|
5
|
+
import {Primitive as $7V4JZ$Primitive} from "@radix-ui/react-primitive";
|
|
6
|
+
import {useId as $7V4JZ$useId} from "@radix-ui/react-id";
|
|
7
|
+
|
|
8
|
+
function $parcel$export(e, n, v, s) {
|
|
9
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
10
|
+
}
|
|
11
|
+
var $b73a6c6685e72184$exports = {};
|
|
12
|
+
|
|
13
|
+
$parcel$export($b73a6c6685e72184$exports, "Label", () => $b73a6c6685e72184$export$b04be29aa201d4f5);
|
|
14
|
+
$parcel$export($b73a6c6685e72184$exports, "useLabelContext", () => $b73a6c6685e72184$export$feddbbf47baabdb9);
|
|
15
|
+
$parcel$export($b73a6c6685e72184$exports, "Root", () => $b73a6c6685e72184$export$be92b6f5f03c0fe9);
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
/* -------------------------------------------------------------------------------------------------
|
|
23
|
+
* Label
|
|
24
|
+
* -----------------------------------------------------------------------------------------------*/ const $b73a6c6685e72184$var$NAME = 'Label';
|
|
25
|
+
const [$b73a6c6685e72184$var$LabelProvider, $b73a6c6685e72184$var$useLabelContextImpl] = $7V4JZ$createContext($b73a6c6685e72184$var$NAME, {
|
|
26
|
+
id: undefined,
|
|
27
|
+
controlRef: {
|
|
28
|
+
current: null
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
const $b73a6c6685e72184$export$b04be29aa201d4f5 = /*#__PURE__*/ $7V4JZ$forwardRef((props, forwardedRef)=>{
|
|
32
|
+
const { htmlFor: htmlFor , id: idProp , ...labelProps } = props;
|
|
33
|
+
const controlRef = $7V4JZ$useRef(null);
|
|
34
|
+
const ref = $7V4JZ$useRef(null);
|
|
35
|
+
const composedRefs = $7V4JZ$useComposedRefs(forwardedRef, ref);
|
|
36
|
+
const id = $7V4JZ$useId(idProp);
|
|
37
|
+
$7V4JZ$useEffect(()=>{
|
|
38
|
+
if (htmlFor) {
|
|
39
|
+
const element = document.getElementById(htmlFor);
|
|
40
|
+
const label = ref.current;
|
|
41
|
+
if (label && element) {
|
|
42
|
+
const getAriaLabel = ()=>element.getAttribute('aria-labelledby')
|
|
43
|
+
;
|
|
44
|
+
const ariaLabelledBy1 = [
|
|
45
|
+
id,
|
|
46
|
+
getAriaLabel()
|
|
47
|
+
].filter(Boolean).join(' ');
|
|
48
|
+
element.setAttribute('aria-labelledby', ariaLabelledBy1);
|
|
49
|
+
controlRef.current = element;
|
|
50
|
+
return ()=>{
|
|
51
|
+
var _getAriaLabel;
|
|
52
|
+
/**
|
|
53
|
+
* We get the latest attribute value because at the time that this cleanup fires,
|
|
54
|
+
* the values from the closure may have changed.
|
|
55
|
+
*/ const ariaLabelledBy = (_getAriaLabel = getAriaLabel()) === null || _getAriaLabel === void 0 ? void 0 : _getAriaLabel.replace(id, '');
|
|
56
|
+
if (ariaLabelledBy === '') element.removeAttribute('aria-labelledby');
|
|
57
|
+
else if (ariaLabelledBy) element.setAttribute('aria-labelledby', ariaLabelledBy);
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}, [
|
|
62
|
+
id,
|
|
63
|
+
htmlFor
|
|
64
|
+
]);
|
|
65
|
+
return /*#__PURE__*/ $7V4JZ$createElement($b73a6c6685e72184$var$LabelProvider, {
|
|
66
|
+
id: id,
|
|
67
|
+
controlRef: controlRef
|
|
68
|
+
}, /*#__PURE__*/ $7V4JZ$createElement($7V4JZ$Primitive.span, $7V4JZ$babelruntimehelpersesmextends({
|
|
69
|
+
role: "label",
|
|
70
|
+
id: id
|
|
71
|
+
}, labelProps, {
|
|
72
|
+
ref: composedRefs,
|
|
73
|
+
onMouseDown: (event)=>{
|
|
74
|
+
var _props$onMouseDown;
|
|
75
|
+
(_props$onMouseDown = props.onMouseDown) === null || _props$onMouseDown === void 0 || _props$onMouseDown.call(props, event); // prevent text selection when double clicking label
|
|
76
|
+
if (!event.defaultPrevented && event.detail > 1) event.preventDefault();
|
|
77
|
+
},
|
|
78
|
+
onClick: (event)=>{
|
|
79
|
+
var _props$onClick;
|
|
80
|
+
(_props$onClick = props.onClick) === null || _props$onClick === void 0 || _props$onClick.call(props, event);
|
|
81
|
+
if (!controlRef.current || event.defaultPrevented) return;
|
|
82
|
+
const isClickingControl = controlRef.current.contains(event.target); // Ensure event was generated by a user action
|
|
83
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted
|
|
84
|
+
const isUserClick = event.isTrusted === true;
|
|
85
|
+
/**
|
|
86
|
+
* When a label is wrapped around the control it labels, we trigger the appropriate events
|
|
87
|
+
* on the control when the label is clicked. We do nothing if the user is already clicking the
|
|
88
|
+
* control inside the label.
|
|
89
|
+
*/ if (!isClickingControl && isUserClick) {
|
|
90
|
+
controlRef.current.click();
|
|
91
|
+
controlRef.current.focus();
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
})));
|
|
95
|
+
});
|
|
96
|
+
/*#__PURE__*/ Object.assign($b73a6c6685e72184$export$b04be29aa201d4f5, {
|
|
97
|
+
displayName: $b73a6c6685e72184$var$NAME
|
|
98
|
+
});
|
|
99
|
+
/* -----------------------------------------------------------------------------------------------*/ const $b73a6c6685e72184$export$feddbbf47baabdb9 = (element)=>{
|
|
100
|
+
const context = $b73a6c6685e72184$var$useLabelContextImpl('LabelConsumer');
|
|
101
|
+
const { controlRef: controlRef } = context;
|
|
102
|
+
$7V4JZ$useEffect(()=>{
|
|
103
|
+
if (element) controlRef.current = element;
|
|
104
|
+
}, [
|
|
105
|
+
element,
|
|
106
|
+
controlRef
|
|
107
|
+
]);
|
|
108
|
+
return context.id;
|
|
109
|
+
};
|
|
110
|
+
const $b73a6c6685e72184$export$be92b6f5f03c0fe9 = $b73a6c6685e72184$export$b04be29aa201d4f5;
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
export {$b73a6c6685e72184$export$b04be29aa201d4f5 as Label, $b73a6c6685e72184$export$feddbbf47baabdb9 as useLabelContext, $b73a6c6685e72184$export$be92b6f5f03c0fe9 as Root};
|
|
2
116
|
//# sourceMappingURL=index.module.js.map
|
package/dist/index.module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;A;;;;;;ACQA;;oGAEA,CAEA,MAAMK,0BAAI,GAAG,OAAb,AAAA;AAGA,MAAM,CAACC,mCAAD,EAAgBC,yCAAhB,CAAA,GAAuCN,oBAAa,CAAoBI,0BAApB,EAA0B;IAClFG,EAAE,EAAEC,SAD8E;IAElFC,UAAU,EAAE;QAAEC,OAAO,EAAE,IAATA;KAAF;CAF4C,CAA1D,AAAoF;AAWpF,MAAMC,yCAAK,GAAA,aAAGZ,CAAAA,iBAAA,CAA2C,CAACc,KAAD,EAAQC,YAAR,GAAyB;IAChF,MAAM,E,SAAEC,OAAF,CAAA,EAAWR,EAAE,EAA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ADA;YAEA,mEAAA;YACA,MAAMC,WAAW,GAAGT,KAAK,CAACU,SAAN,KAAoB,IAAxC,AAAA;YACA;;;;SAIV,CACU,IAAI,CAACJ,iBAAD,IAAsBG,WAA1B,EAAuC;gBACrCnC,UAAU,CAACC,OAAX,CAAmBoC,KAAnB,EAAArC,CAAAA;gBACAA,UAAU,CAACC,OAAX,CAAmBqC,KAAnB,EAAAtC,CAAAA;aACD;SACF;KA1BH,CAAA,CADF,CADF,CAEI;CAlCQ,CAAd,AAgEC;AAED,aAAA,CAAA,MAAA,CAAA,MAAA,CAAA,yCAAA,EAAA;IAAA,WAAA,EAAA,0BAAA;CAAA,CAAA,CAAA;AAEA,oGAAA,CAEA,MAAMuC,yCAAe,GAAI1B,CAAAA,OAAD,GAAkC;IACxD,MAAM2B,OAAO,GAAG3C,yCAAmB,CAAC,eAAD,CAAnC,AAAA;IACA,MAAM,E,YAAEG,UAAAA,CAAAA,EAAF,GAAiBwC,OAAvB,AAAM;IAENlD,gBAAA,CAAgB,IAAM;QACpB,IAAIuB,OAAJ,EAAab,UAAU,CAACC,OAAX,GAAqBY,OAArB,CAAb;KADF,EAEG;QAACA,OAAD;QAAUb,UAAV;KAFH,CAEC,CAAA;IAED,OAAOwC,OAAO,CAAC1C,EAAf,CAAA;CARF,AASC;AAED,MAAM2C,yCAAI,GAAGvC,yCAAb,AAAA;;AD3GA","sources":["packages/react/label/src/index.ts","packages/react/label/src/Label.tsx"],"sourcesContent":["export * from './Label';\n","import * as React from 'react';\nimport { createContext } from '@radix-ui/react-context';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { Primitive } from '@radix-ui/react-primitive';\nimport { useId } from '@radix-ui/react-id';\n\nimport type * as Radix from '@radix-ui/react-primitive';\n\n/* -------------------------------------------------------------------------------------------------\n * Label\n * -----------------------------------------------------------------------------------------------*/\n\nconst NAME = 'Label';\n\ntype LabelContextValue = { id?: string; controlRef: React.MutableRefObject<HTMLElement | null> };\nconst [LabelProvider, useLabelContextImpl] = createContext<LabelContextValue>(NAME, {\n id: undefined,\n controlRef: { current: null },\n});\n\ntype LabelElement = React.ElementRef<typeof Primitive.span>;\ntype PrimitiveSpanProps = Radix.ComponentPropsWithoutRef<typeof Primitive.span>;\ninterface LabelProps extends PrimitiveSpanProps {\n htmlFor?: string;\n}\n\nconst Label = React.forwardRef<LabelElement, LabelProps>((props, forwardedRef) => {\n const { htmlFor, id: idProp, ...labelProps } = props;\n const controlRef = React.useRef<HTMLElement | null>(null);\n const ref = React.useRef<HTMLSpanElement>(null);\n const composedRefs = useComposedRefs(forwardedRef, ref);\n const id = useId(idProp);\n\n React.useEffect(() => {\n if (htmlFor) {\n const element = document.getElementById(htmlFor);\n const label = ref.current;\n if (label && element) {\n const getAriaLabel = () => element.getAttribute('aria-labelledby');\n const ariaLabelledBy = [id, getAriaLabel()].filter(Boolean).join(' ');\n element.setAttribute('aria-labelledby', ariaLabelledBy);\n controlRef.current = element;\n return () => {\n /**\n * We get the latest attribute value because at the time that this cleanup fires,\n * the values from the closure may have changed.\n */\n const ariaLabelledBy = getAriaLabel()?.replace(id, '');\n if (ariaLabelledBy === '') {\n element.removeAttribute('aria-labelledby');\n } else if (ariaLabelledBy) {\n element.setAttribute('aria-labelledby', ariaLabelledBy);\n }\n };\n }\n }\n }, [id, htmlFor]);\n\n return (\n <LabelProvider id={id} controlRef={controlRef}>\n <Primitive.span\n role=\"label\"\n id={id}\n {...labelProps}\n ref={composedRefs}\n onMouseDown={(event) => {\n props.onMouseDown?.(event);\n // prevent text selection when double clicking label\n if (!event.defaultPrevented && event.detail > 1) event.preventDefault();\n }}\n onClick={(event) => {\n props.onClick?.(event);\n if (!controlRef.current || event.defaultPrevented) return;\n const isClickingControl = controlRef.current.contains(event.target as Node);\n // Ensure event was generated by a user action\n // https://developer.mozilla.org/en-US/docs/Web/API/Event/isTrusted\n const isUserClick = event.isTrusted === true;\n /**\n * When a label is wrapped around the control it labels, we trigger the appropriate events\n * on the control when the label is clicked. We do nothing if the user is already clicking the\n * control inside the label.\n */\n if (!isClickingControl && isUserClick) {\n controlRef.current.click();\n controlRef.current.focus();\n }\n }}\n />\n </LabelProvider>\n );\n});\n\nLabel.displayName = NAME;\n\n/* -----------------------------------------------------------------------------------------------*/\n\nconst useLabelContext = (element?: HTMLElement | null) => {\n const context = useLabelContextImpl('LabelConsumer');\n const { controlRef } = context;\n\n React.useEffect(() => {\n if (element) controlRef.current = element;\n }, [element, controlRef]);\n\n return context.id;\n};\n\nconst Root = Label;\n\nexport {\n Label,\n //\n Root,\n //\n useLabelContext,\n};\nexport type { LabelProps };\n"],"names":["React","createContext","useComposedRefs","Primitive","useId","NAME","LabelProvider","useLabelContextImpl","id","undefined","controlRef","current","Label","forwardRef","props","forwardedRef","htmlFor","idProp","labelProps","useRef","ref","composedRefs","useEffect","element","document","getElementById","label","getAriaLabel","getAttribute","ariaLabelledBy","filter","Boolean","join","setAttribute","replace","removeAttribute","event","onMouseDown","defaultPrevented","detail","preventDefault","onClick","isClickingControl","contains","target","isUserClick","isTrusted","click","focus","useLabelContext","context","Root"],"version":3,"file":"index.module.js.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@radix-ui/react-label",
|
|
3
|
-
"version": "0.1.6-rc.
|
|
3
|
+
"version": "0.1.6-rc.5",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"source": "src/index.ts",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -17,10 +17,10 @@
|
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
19
|
"@babel/runtime": "^7.13.10",
|
|
20
|
-
"@radix-ui/react-compose-refs": "0.1.1-rc.
|
|
21
|
-
"@radix-ui/react-context": "0.1.2-rc.
|
|
22
|
-
"@radix-ui/react-id": "0.1.6-rc.
|
|
23
|
-
"@radix-ui/react-primitive": "0.1.5-rc.
|
|
20
|
+
"@radix-ui/react-compose-refs": "0.1.1-rc.5",
|
|
21
|
+
"@radix-ui/react-context": "0.1.2-rc.5",
|
|
22
|
+
"@radix-ui/react-id": "0.1.6-rc.5",
|
|
23
|
+
"@radix-ui/react-primitive": "0.1.5-rc.5"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
26
|
"react": "^16.8 || ^17.0 || ^18.0"
|