@radix-ui/primitive 1.2.0 → 1.2.1-rc.1791984254107
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 +41 -2
- package/dist/index.d.ts +41 -2
- package/dist/index.js +157 -2
- package/dist/index.js.map +3 -3
- package/dist/index.mjs +157 -2
- package/dist/index.mjs.map +3 -3
- package/package.json +6 -33
- package/src/aria-hidden.test.tsx +556 -0
- package/src/aria-hidden.ts +242 -0
- package/src/can-use-dom.ts +6 -0
- package/src/index.ts +4 -0
- package/src/primitive.tsx +68 -0
- package/src/types.ts +3 -0
- package/dist/internal/is-development.false.d.mts +0 -3
- package/dist/internal/is-development.false.d.ts +0 -3
- package/dist/internal/is-development.false.js +0 -27
- package/dist/internal/is-development.false.js.map +0 -7
- package/dist/internal/is-development.false.mjs +0 -6
- package/dist/internal/is-development.false.mjs.map +0 -7
- package/dist/internal/is-development.true.d.mts +0 -3
- package/dist/internal/is-development.true.d.ts +0 -3
- package/dist/internal/is-development.true.js +0 -27
- package/dist/internal/is-development.true.js.map +0 -7
- package/dist/internal/is-development.true.mjs +0 -6
- package/dist/internal/is-development.true.mjs.map +0 -7
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
declare const canUseDOM: boolean;
|
|
2
1
|
declare function composeEventHandlers<E extends {
|
|
3
2
|
defaultPrevented: boolean;
|
|
4
3
|
}>(originalEventHandler?: (event: E) => void, ourEventHandler?: (event: E) => void, { checkForDefaultPrevented }?: {
|
|
@@ -13,8 +12,48 @@ declare function getOwnerDocument(element: Node | null | undefined): Document;
|
|
|
13
12
|
declare function getActiveElement(node: Node | null | undefined, activeDescendant?: boolean): HTMLElement | null;
|
|
14
13
|
declare function isFrame(element: Element): element is HTMLIFrameElement;
|
|
15
14
|
|
|
15
|
+
declare const canUseDOM: boolean;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Fork of `aria-hidden`
|
|
19
|
+
*
|
|
20
|
+
* MIT License
|
|
21
|
+
* Copyright (c) 2017 Anton Korzunov
|
|
22
|
+
*/
|
|
23
|
+
type Undo = () => void;
|
|
24
|
+
type DomParentNode = Element | ShadowRoot;
|
|
25
|
+
/**
|
|
26
|
+
* Marks everything except given nodes as aria-hidden
|
|
27
|
+
*
|
|
28
|
+
* @param originalTargets - elements to keep on the page
|
|
29
|
+
* @param parentNode - top element
|
|
30
|
+
* @param markerName - a special attribute to mark every node
|
|
31
|
+
* @return undo command
|
|
32
|
+
*/
|
|
33
|
+
declare function hideOthers(originalTargets: Set<Element>, parentNode: DomParentNode, markerName?: string): Undo;
|
|
34
|
+
/**
|
|
35
|
+
* Marks everything except given nodes as inert
|
|
36
|
+
*
|
|
37
|
+
* @param originalTargets - elements to keep on the page
|
|
38
|
+
* @param parentNode - top element
|
|
39
|
+
* @param markerName - a special attribute to mark every node
|
|
40
|
+
* @return undo command
|
|
41
|
+
*/
|
|
42
|
+
declare function inertOthers(originalTargets: Set<Element>, parentNode: DomParentNode, markerName?: string): Undo;
|
|
43
|
+
declare function supportsInert(): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Automatic function to "suppress" DOM elements - _hide_ or _inert_ in the best
|
|
46
|
+
* possible way.
|
|
47
|
+
*
|
|
48
|
+
* @param originalTargets - elements to keep on the page
|
|
49
|
+
* @param parentNode - top element
|
|
50
|
+
* @param markerName - a special attribute to mark every node
|
|
51
|
+
* @return undo command
|
|
52
|
+
*/
|
|
53
|
+
declare function suppressOthers(originalTargets: Set<Element>, parentNode: DomParentNode, markerName?: string): Undo;
|
|
54
|
+
|
|
16
55
|
type Timeout = ReturnType<typeof setTimeout>;
|
|
17
56
|
type Interval = ReturnType<typeof setInterval>;
|
|
18
57
|
type Immediate = ReturnType<typeof setImmediate>;
|
|
19
58
|
|
|
20
|
-
export { type Immediate, type Interval, type Timeout, canUseDOM, composeEventHandlers, getActiveElement, getOwnerDocument, getOwnerWindow, isFrame };
|
|
59
|
+
export { type Immediate, type Interval, type Timeout, type Undo, canUseDOM, composeEventHandlers, getActiveElement, getOwnerDocument, getOwnerWindow, hideOthers, inertOthers, isFrame, supportsInert, suppressOthers };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
declare const canUseDOM: boolean;
|
|
2
1
|
declare function composeEventHandlers<E extends {
|
|
3
2
|
defaultPrevented: boolean;
|
|
4
3
|
}>(originalEventHandler?: (event: E) => void, ourEventHandler?: (event: E) => void, { checkForDefaultPrevented }?: {
|
|
@@ -13,8 +12,48 @@ declare function getOwnerDocument(element: Node | null | undefined): Document;
|
|
|
13
12
|
declare function getActiveElement(node: Node | null | undefined, activeDescendant?: boolean): HTMLElement | null;
|
|
14
13
|
declare function isFrame(element: Element): element is HTMLIFrameElement;
|
|
15
14
|
|
|
15
|
+
declare const canUseDOM: boolean;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Fork of `aria-hidden`
|
|
19
|
+
*
|
|
20
|
+
* MIT License
|
|
21
|
+
* Copyright (c) 2017 Anton Korzunov
|
|
22
|
+
*/
|
|
23
|
+
type Undo = () => void;
|
|
24
|
+
type DomParentNode = Element | ShadowRoot;
|
|
25
|
+
/**
|
|
26
|
+
* Marks everything except given nodes as aria-hidden
|
|
27
|
+
*
|
|
28
|
+
* @param originalTargets - elements to keep on the page
|
|
29
|
+
* @param parentNode - top element
|
|
30
|
+
* @param markerName - a special attribute to mark every node
|
|
31
|
+
* @return undo command
|
|
32
|
+
*/
|
|
33
|
+
declare function hideOthers(originalTargets: Set<Element>, parentNode: DomParentNode, markerName?: string): Undo;
|
|
34
|
+
/**
|
|
35
|
+
* Marks everything except given nodes as inert
|
|
36
|
+
*
|
|
37
|
+
* @param originalTargets - elements to keep on the page
|
|
38
|
+
* @param parentNode - top element
|
|
39
|
+
* @param markerName - a special attribute to mark every node
|
|
40
|
+
* @return undo command
|
|
41
|
+
*/
|
|
42
|
+
declare function inertOthers(originalTargets: Set<Element>, parentNode: DomParentNode, markerName?: string): Undo;
|
|
43
|
+
declare function supportsInert(): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Automatic function to "suppress" DOM elements - _hide_ or _inert_ in the best
|
|
46
|
+
* possible way.
|
|
47
|
+
*
|
|
48
|
+
* @param originalTargets - elements to keep on the page
|
|
49
|
+
* @param parentNode - top element
|
|
50
|
+
* @param markerName - a special attribute to mark every node
|
|
51
|
+
* @return undo command
|
|
52
|
+
*/
|
|
53
|
+
declare function suppressOthers(originalTargets: Set<Element>, parentNode: DomParentNode, markerName?: string): Undo;
|
|
54
|
+
|
|
16
55
|
type Timeout = ReturnType<typeof setTimeout>;
|
|
17
56
|
type Interval = ReturnType<typeof setInterval>;
|
|
18
57
|
type Immediate = ReturnType<typeof setImmediate>;
|
|
19
58
|
|
|
20
|
-
export { type Immediate, type Interval, type Timeout, canUseDOM, composeEventHandlers, getActiveElement, getOwnerDocument, getOwnerWindow, isFrame };
|
|
59
|
+
export { type Immediate, type Interval, type Timeout, type Undo, canUseDOM, composeEventHandlers, getActiveElement, getOwnerDocument, getOwnerWindow, hideOthers, inertOthers, isFrame, supportsInert, suppressOthers };
|
package/dist/index.js
CHANGED
|
@@ -26,12 +26,18 @@ __export(index_exports, {
|
|
|
26
26
|
getActiveElement: () => getActiveElement,
|
|
27
27
|
getOwnerDocument: () => getOwnerDocument,
|
|
28
28
|
getOwnerWindow: () => getOwnerWindow,
|
|
29
|
-
|
|
29
|
+
hideOthers: () => hideOthers,
|
|
30
|
+
inertOthers: () => inertOthers,
|
|
31
|
+
isFrame: () => isFrame,
|
|
32
|
+
supportsInert: () => supportsInert,
|
|
33
|
+
suppressOthers: () => suppressOthers
|
|
30
34
|
});
|
|
31
35
|
module.exports = __toCommonJS(index_exports);
|
|
32
36
|
|
|
33
|
-
// src/
|
|
37
|
+
// src/can-use-dom.ts
|
|
34
38
|
var canUseDOM = !!(typeof window !== "undefined" && window.document && window.document.createElement);
|
|
39
|
+
|
|
40
|
+
// src/primitive.tsx
|
|
35
41
|
function composeEventHandlers(originalEventHandler, ourEventHandler, { checkForDefaultPrevented = true } = {}) {
|
|
36
42
|
return /* @__PURE__ */ __name(function handleEvent(event) {
|
|
37
43
|
originalEventHandler?.(event);
|
|
@@ -79,4 +85,153 @@ function isFrame(element) {
|
|
|
79
85
|
return element.tagName === "IFRAME";
|
|
80
86
|
}
|
|
81
87
|
__name(isFrame, "isFrame");
|
|
88
|
+
|
|
89
|
+
// src/aria-hidden.ts
|
|
90
|
+
var import_is_development = require("@radix-ui/env-check/is-development");
|
|
91
|
+
var counterMap = /* @__PURE__ */ new WeakMap();
|
|
92
|
+
var uncontrolledNodes = /* @__PURE__ */ new WeakMap();
|
|
93
|
+
var lockCount = 0;
|
|
94
|
+
var markerMap = /* @__PURE__ */ new Map();
|
|
95
|
+
var SKIPPED_ELEMENTS = /* @__PURE__ */ new Set([
|
|
96
|
+
//
|
|
97
|
+
"link",
|
|
98
|
+
"meta",
|
|
99
|
+
"noscript",
|
|
100
|
+
"script",
|
|
101
|
+
"style",
|
|
102
|
+
"template"
|
|
103
|
+
]);
|
|
104
|
+
function unwrapHost(node) {
|
|
105
|
+
return node && (node.host || unwrapHost(node.parentNode));
|
|
106
|
+
}
|
|
107
|
+
__name(unwrapHost, "unwrapHost");
|
|
108
|
+
function correctTargets(parent, targets) {
|
|
109
|
+
const correctedTargets = /* @__PURE__ */ new Set();
|
|
110
|
+
for (const target of targets) {
|
|
111
|
+
if (parent.contains(target)) {
|
|
112
|
+
correctedTargets.add(target);
|
|
113
|
+
} else {
|
|
114
|
+
const correctedTarget = unwrapHost(target);
|
|
115
|
+
if (correctedTarget && parent.contains(correctedTarget)) {
|
|
116
|
+
correctedTargets.add(correctedTarget);
|
|
117
|
+
} else {
|
|
118
|
+
if (import_is_development.IS_DEVELOPMENT) {
|
|
119
|
+
console.error(
|
|
120
|
+
"aria-hidden",
|
|
121
|
+
target,
|
|
122
|
+
"in not contained inside",
|
|
123
|
+
parent,
|
|
124
|
+
". Doing nothing"
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return correctedTargets;
|
|
131
|
+
}
|
|
132
|
+
__name(correctTargets, "correctTargets");
|
|
133
|
+
function applyAttributeToOthers(originalTargets, parentNode, markerName, controlAttribute) {
|
|
134
|
+
const targets = correctTargets(parentNode, originalTargets);
|
|
135
|
+
const additionalTargets = parentNode.querySelectorAll("[aria-live]");
|
|
136
|
+
for (const target of additionalTargets) {
|
|
137
|
+
targets.add(target);
|
|
138
|
+
}
|
|
139
|
+
let markerCounter = markerMap.get(markerName);
|
|
140
|
+
if (!markerCounter) {
|
|
141
|
+
markerCounter = /* @__PURE__ */ new WeakMap();
|
|
142
|
+
markerMap.set(markerName, markerCounter);
|
|
143
|
+
}
|
|
144
|
+
const hiddenNodes = /* @__PURE__ */ new Set();
|
|
145
|
+
const elementsToKeep = /* @__PURE__ */ new Set();
|
|
146
|
+
const elementsToStop = new Set(targets);
|
|
147
|
+
targets.forEach(keep);
|
|
148
|
+
deep(parentNode);
|
|
149
|
+
elementsToKeep.clear();
|
|
150
|
+
lockCount++;
|
|
151
|
+
return () => {
|
|
152
|
+
for (const node of hiddenNodes) {
|
|
153
|
+
const counterValue = counterMap.get(node) - 1;
|
|
154
|
+
const markerValue = markerCounter.get(node) - 1;
|
|
155
|
+
counterMap.set(node, counterValue);
|
|
156
|
+
markerCounter.set(node, markerValue);
|
|
157
|
+
if (!counterValue) {
|
|
158
|
+
if (!uncontrolledNodes.has(node)) {
|
|
159
|
+
node.removeAttribute(controlAttribute);
|
|
160
|
+
}
|
|
161
|
+
uncontrolledNodes.delete(node);
|
|
162
|
+
}
|
|
163
|
+
if (!markerValue) {
|
|
164
|
+
node.removeAttribute(markerName);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
lockCount--;
|
|
168
|
+
if (!lockCount) {
|
|
169
|
+
counterMap = /* @__PURE__ */ new WeakMap();
|
|
170
|
+
uncontrolledNodes = /* @__PURE__ */ new WeakMap();
|
|
171
|
+
markerMap.clear();
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
function keep(el) {
|
|
175
|
+
if (!el || elementsToKeep.has(el)) {
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
elementsToKeep.add(el);
|
|
179
|
+
keep(el.parentNode);
|
|
180
|
+
}
|
|
181
|
+
__name(keep, "keep");
|
|
182
|
+
function deep(parent) {
|
|
183
|
+
if (!parent || elementsToStop.has(parent)) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
for (const node of parent.children) {
|
|
187
|
+
if (SKIPPED_ELEMENTS.has(node.localName)) {
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
if (elementsToKeep.has(node)) {
|
|
191
|
+
deep(node);
|
|
192
|
+
} else {
|
|
193
|
+
try {
|
|
194
|
+
const attr = node.getAttribute(controlAttribute);
|
|
195
|
+
const alreadyHidden = controlAttribute === "inert" ? attr !== null : attr !== null && attr !== "false";
|
|
196
|
+
const counterValue = (counterMap.get(node) || 0) + 1;
|
|
197
|
+
const markerValue = (markerCounter?.get(node) || 0) + 1;
|
|
198
|
+
counterMap.set(node, counterValue);
|
|
199
|
+
markerCounter?.set(node, markerValue);
|
|
200
|
+
hiddenNodes.add(node);
|
|
201
|
+
if (counterValue === 1 && alreadyHidden) {
|
|
202
|
+
uncontrolledNodes.set(node, true);
|
|
203
|
+
}
|
|
204
|
+
if (markerValue === 1) {
|
|
205
|
+
node.setAttribute(markerName, "true");
|
|
206
|
+
}
|
|
207
|
+
if (!alreadyHidden) {
|
|
208
|
+
node.setAttribute(controlAttribute, "true");
|
|
209
|
+
}
|
|
210
|
+
} catch (e) {
|
|
211
|
+
if (import_is_development.IS_DEVELOPMENT) {
|
|
212
|
+
console.error("aria-hidden: cannot operate on ", node, e);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
__name(deep, "deep");
|
|
219
|
+
}
|
|
220
|
+
__name(applyAttributeToOthers, "applyAttributeToOthers");
|
|
221
|
+
function hideOthers(originalTargets, parentNode, markerName = "data-aria-hidden") {
|
|
222
|
+
return applyAttributeToOthers(originalTargets, parentNode, markerName, "aria-hidden");
|
|
223
|
+
}
|
|
224
|
+
__name(hideOthers, "hideOthers");
|
|
225
|
+
function inertOthers(originalTargets, parentNode, markerName = "data-inert-ed") {
|
|
226
|
+
return applyAttributeToOthers(originalTargets, parentNode, markerName, "inert");
|
|
227
|
+
}
|
|
228
|
+
__name(inertOthers, "inertOthers");
|
|
229
|
+
function supportsInert() {
|
|
230
|
+
return typeof HTMLElement !== "undefined" && HTMLElement.prototype.hasOwnProperty("inert");
|
|
231
|
+
}
|
|
232
|
+
__name(supportsInert, "supportsInert");
|
|
233
|
+
function suppressOthers(originalTargets, parentNode, markerName = "data-suppressed") {
|
|
234
|
+
return (supportsInert() ? inertOthers : hideOthers)(originalTargets, parentNode, markerName);
|
|
235
|
+
}
|
|
236
|
+
__name(suppressOthers, "suppressOthers");
|
|
82
237
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/index.ts", "../src/primitive.tsx"],
|
|
4
|
-
"sourcesContent": ["export * from './primitive';\nexport type * from './types';\n", "/*
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;
|
|
3
|
+
"sources": ["../src/index.ts", "../src/can-use-dom.ts", "../src/primitive.tsx", "../src/aria-hidden.ts"],
|
|
4
|
+
"sourcesContent": ["export * from './primitive';\nexport * from './can-use-dom';\nexport * from './aria-hidden';\nexport type * from './types';\n", "/* oxlint-disable no-restricted-globals */\nexport const canUseDOM = !!(\n typeof window !== 'undefined' &&\n window.document &&\n window.document.createElement\n);\n", "/* oxlint-disable no-restricted-properties */\nimport { canUseDOM } from './can-use-dom';\n\nexport function composeEventHandlers<E extends { defaultPrevented: boolean }>(\n originalEventHandler?: (event: E) => void,\n ourEventHandler?: (event: E) => void,\n { checkForDefaultPrevented = true }: { checkForDefaultPrevented?: boolean | undefined } = {},\n) {\n return function handleEvent(event: E) {\n originalEventHandler?.(event);\n\n if (checkForDefaultPrevented === false || !event || !event.defaultPrevented) {\n return ourEventHandler?.(event);\n }\n };\n}\n\nexport function getOwnerWindow(element: Node | null | undefined) {\n if (!canUseDOM) {\n throw new Error('Cannot access window outside of the DOM');\n }\n // oxlint-disable-next-line no-restricted-globals\n return element?.ownerDocument?.defaultView ?? window;\n}\n\nexport function getOwnerDocument(element: Node | null | undefined) {\n if (!canUseDOM) {\n throw new Error('Cannot access document outside of the DOM');\n }\n // oxlint-disable-next-line no-restricted-globals\n return element?.ownerDocument ?? document;\n}\n\n/**\n * Lifted from https://github.com/ariakit/ariakit/blob/main/packages/ariakit-core/src/utils/dom.ts#L37\n * MIT License, Copyright (c) AriaKit.\n */\nexport function getActiveElement(\n node: Node | null | undefined,\n activeDescendant = false,\n): HTMLElement | null {\n const { activeElement } = getOwnerDocument(node);\n if (!activeElement?.nodeName) {\n // `activeElement` might be an empty object if we're interacting with elements\n // inside of an iframe.\n return null;\n }\n\n if (isFrame(activeElement) && activeElement.contentDocument) {\n return getActiveElement(activeElement.contentDocument.body, activeDescendant);\n }\n\n if (activeDescendant) {\n const id = activeElement.getAttribute('aria-activedescendant');\n if (id) {\n const element = getOwnerDocument(activeElement).getElementById(id);\n if (element) {\n return element;\n }\n }\n }\n\n return activeElement as HTMLElement | null;\n}\n\nexport function isFrame(element: Element): element is HTMLIFrameElement {\n return element.tagName === 'IFRAME';\n}\n", "/**\n * Fork of `aria-hidden`\n *\n * MIT License\n * Copyright (c) 2017 Anton Korzunov\n */\n\nimport { IS_DEVELOPMENT } from '@radix-ui/env-check/is-development';\n\ntype Undo = () => void;\n\ntype DomParentNode = Element | ShadowRoot;\n\nlet counterMap = /* @__PURE__ */ new WeakMap<Element, number>();\nlet uncontrolledNodes = /* @__PURE__ */ new WeakMap<Element, boolean>();\nlet lockCount = /* @__PURE__ */ 0;\nconst markerMap = /* @__PURE__ */ new Map<string, WeakMap<Element, number>>();\n\n// Elements that are never rendered and are not in the accessibility tree, so\n// marking them is wasted work.\n//\n// See https://html.spec.whatwg.org/multipage/rendering.html#hidden-elements\nconst SKIPPED_ELEMENTS = new Set([\n //\n 'link',\n 'meta',\n 'noscript',\n 'script',\n 'style',\n 'template',\n]);\n\nfunction unwrapHost(node: DomParentNode): Element | null {\n return node && ((node as ShadowRoot).host || unwrapHost(node.parentNode as Element));\n}\n\nfunction correctTargets(parent: DomParentNode, targets: Set<Element>): Set<Element> {\n const correctedTargets = new Set<Element>();\n for (const target of targets) {\n if (parent.contains(target)) {\n correctedTargets.add(target);\n } else {\n const correctedTarget = unwrapHost(target);\n if (correctedTarget && parent.contains(correctedTarget)) {\n correctedTargets.add(correctedTarget);\n } else {\n if (IS_DEVELOPMENT) {\n console.error(\n 'aria-hidden',\n target,\n 'in not contained inside',\n parent,\n '. Doing nothing',\n );\n }\n }\n }\n }\n\n return correctedTargets;\n}\n\n/**\n * Marks everything except given node(or nodes) as aria-hidden\n * @param originalTargets - elements to keep on the page\n * @param parentNode - top element\n * @param markerName - a special attribute to mark every node\n * @param controlAttribute - html Attribute to control\n * @return undo command\n */\nfunction applyAttributeToOthers(\n originalTargets: Set<Element>,\n parentNode: DomParentNode,\n markerName: string,\n controlAttribute: string,\n): Undo {\n const targets = correctTargets(parentNode, originalTargets);\n\n // We should not hide aria-live elements\n //\n // See https://github.com/theKashey/aria-hidden/issues/10\n const additionalTargets = parentNode.querySelectorAll('[aria-live]');\n for (const target of additionalTargets) {\n targets.add(target);\n }\n\n let markerCounter = markerMap.get(markerName);\n if (!markerCounter) {\n markerCounter = new WeakMap();\n markerMap.set(markerName, markerCounter);\n }\n\n const hiddenNodes = new Set<Element>();\n const elementsToKeep = new Set<Node>();\n const elementsToStop = new Set<Node>(targets);\n\n targets.forEach(keep);\n deep(parentNode);\n elementsToKeep.clear();\n lockCount++;\n\n return () => {\n for (const node of hiddenNodes) {\n const counterValue = counterMap.get(node)! - 1;\n const markerValue = markerCounter.get(node)! - 1;\n\n counterMap.set(node, counterValue);\n markerCounter.set(node, markerValue);\n\n if (!counterValue) {\n if (!uncontrolledNodes.has(node)) {\n node.removeAttribute(controlAttribute);\n }\n\n uncontrolledNodes.delete(node);\n }\n\n if (!markerValue) {\n node.removeAttribute(markerName);\n }\n }\n\n lockCount--;\n\n if (!lockCount) {\n // clear\n counterMap = new WeakMap();\n uncontrolledNodes = new WeakMap();\n markerMap.clear();\n }\n };\n\n function keep(el: Node | undefined) {\n if (!el || elementsToKeep.has(el)) {\n return;\n }\n\n elementsToKeep.add(el);\n keep(el.parentNode!);\n }\n\n function deep(parent: DomParentNode | null) {\n if (!parent || elementsToStop.has(parent)) {\n return;\n }\n\n for (const node of parent.children) {\n if (SKIPPED_ELEMENTS.has(node.localName)) {\n continue;\n }\n\n if (elementsToKeep.has(node)) {\n deep(node);\n } else {\n try {\n const attr = node.getAttribute(controlAttribute);\n // `inert` is a boolean attribute, so `inert=\"false\"` is still inert\n const alreadyHidden =\n controlAttribute === 'inert' ? attr !== null : attr !== null && attr !== 'false';\n const counterValue = (counterMap.get(node) || 0) + 1;\n const markerValue = (markerCounter?.get(node) || 0) + 1;\n\n counterMap.set(node, counterValue);\n markerCounter?.set(node, markerValue);\n hiddenNodes.add(node);\n\n if (counterValue === 1 && alreadyHidden) {\n uncontrolledNodes.set(node, true);\n }\n\n if (markerValue === 1) {\n node.setAttribute(markerName, 'true');\n }\n\n if (!alreadyHidden) {\n node.setAttribute(controlAttribute, 'true');\n }\n } catch (e) {\n if (IS_DEVELOPMENT) {\n console.error('aria-hidden: cannot operate on ', node, e);\n }\n }\n }\n }\n }\n}\n\n/**\n * Marks everything except given nodes as aria-hidden\n *\n * @param originalTargets - elements to keep on the page\n * @param parentNode - top element\n * @param markerName - a special attribute to mark every node\n * @return undo command\n */\nfunction hideOthers(\n originalTargets: Set<Element>,\n parentNode: DomParentNode,\n markerName = 'data-aria-hidden',\n): Undo {\n return applyAttributeToOthers(originalTargets, parentNode, markerName, 'aria-hidden');\n}\n\n/**\n * Marks everything except given nodes as inert\n *\n * @param originalTargets - elements to keep on the page\n * @param parentNode - top element\n * @param markerName - a special attribute to mark every node\n * @return undo command\n */\nfunction inertOthers(\n originalTargets: Set<Element>,\n parentNode: DomParentNode,\n markerName = 'data-inert-ed',\n): Undo {\n return applyAttributeToOthers(originalTargets, parentNode, markerName, 'inert');\n}\n\nfunction supportsInert(): boolean {\n return typeof HTMLElement !== 'undefined' && HTMLElement.prototype.hasOwnProperty('inert');\n}\n\n/**\n * Automatic function to \"suppress\" DOM elements - _hide_ or _inert_ in the best\n * possible way.\n *\n * @param originalTargets - elements to keep on the page\n * @param parentNode - top element\n * @param markerName - a special attribute to mark every node\n * @return undo command\n */\nfunction suppressOthers(\n originalTargets: Set<Element>,\n parentNode: DomParentNode,\n markerName = 'data-suppressed',\n): Undo {\n return (supportsInert() ? inertOthers : hideOthers)(originalTargets, parentNode, markerName);\n}\n\nexport { hideOthers, inertOthers, supportsInert, suppressOthers };\nexport type { Undo };\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCO,IAAM,YAAY,CAAC,EACxB,OAAO,WAAW,eAClB,OAAO,YACP,OAAO,SAAS;;;ACDX,SAAS,qBACd,sBACA,iBACA,EAAE,2BAA2B,KAAK,IAAwD,CAAC,GAC3F;AACA,SAAO,gCAAS,YAAY,OAAU;AACpC,2BAAuB,KAAK;AAE5B,QAAI,6BAA6B,SAAS,CAAC,SAAS,CAAC,MAAM,kBAAkB;AAC3E,aAA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AQ,MAAM,mCAAmC,MAAM,CAAC;AAAA,UAC1D;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AA3CS;AA4CX;AAnHS;AA6HT,SAAS,WACP,iBACA,YACA,aAAa,oBACP;AACN,SAAO,uBAAuB,iBAAiB,YAAY,YAAY,aAAa;AACtF;AANS;AAgBT,SAAS,YACP,iBACA,YACA,aAAa,iBACP;AACN,SAAO,uBAAuB,iBAAiB,YAAY,YAAY,OAAO;AAChF;AANS;AAQT,SAAS,gBAAyB;AAChC,SAAO,OAAO,gBAAgB,eAAe,YAAY,UAAU,eAAe,OAAO;AAC3F;AAFS;AAaT,SAAS,eACP,iBACA,YACA,aAAa,mBACP;AACN,UAAQ,cAAc,IAAI,cAAc,YAAY,iBAAiB,YAAY,UAAU;AAC7F;AANS;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
3
|
|
|
4
|
-
// src/
|
|
4
|
+
// src/can-use-dom.ts
|
|
5
5
|
var canUseDOM = !!(typeof window !== "undefined" && window.document && window.document.createElement);
|
|
6
|
+
|
|
7
|
+
// src/primitive.tsx
|
|
6
8
|
function composeEventHandlers(originalEventHandler, ourEventHandler, { checkForDefaultPrevented = true } = {}) {
|
|
7
9
|
return /* @__PURE__ */ __name(function handleEvent(event) {
|
|
8
10
|
originalEventHandler?.(event);
|
|
@@ -50,12 +52,165 @@ function isFrame(element) {
|
|
|
50
52
|
return element.tagName === "IFRAME";
|
|
51
53
|
}
|
|
52
54
|
__name(isFrame, "isFrame");
|
|
55
|
+
|
|
56
|
+
// src/aria-hidden.ts
|
|
57
|
+
import { IS_DEVELOPMENT } from "@radix-ui/env-check/is-development";
|
|
58
|
+
var counterMap = /* @__PURE__ */ new WeakMap();
|
|
59
|
+
var uncontrolledNodes = /* @__PURE__ */ new WeakMap();
|
|
60
|
+
var lockCount = 0;
|
|
61
|
+
var markerMap = /* @__PURE__ */ new Map();
|
|
62
|
+
var SKIPPED_ELEMENTS = /* @__PURE__ */ new Set([
|
|
63
|
+
//
|
|
64
|
+
"link",
|
|
65
|
+
"meta",
|
|
66
|
+
"noscript",
|
|
67
|
+
"script",
|
|
68
|
+
"style",
|
|
69
|
+
"template"
|
|
70
|
+
]);
|
|
71
|
+
function unwrapHost(node) {
|
|
72
|
+
return node && (node.host || unwrapHost(node.parentNode));
|
|
73
|
+
}
|
|
74
|
+
__name(unwrapHost, "unwrapHost");
|
|
75
|
+
function correctTargets(parent, targets) {
|
|
76
|
+
const correctedTargets = /* @__PURE__ */ new Set();
|
|
77
|
+
for (const target of targets) {
|
|
78
|
+
if (parent.contains(target)) {
|
|
79
|
+
correctedTargets.add(target);
|
|
80
|
+
} else {
|
|
81
|
+
const correctedTarget = unwrapHost(target);
|
|
82
|
+
if (correctedTarget && parent.contains(correctedTarget)) {
|
|
83
|
+
correctedTargets.add(correctedTarget);
|
|
84
|
+
} else {
|
|
85
|
+
if (IS_DEVELOPMENT) {
|
|
86
|
+
console.error(
|
|
87
|
+
"aria-hidden",
|
|
88
|
+
target,
|
|
89
|
+
"in not contained inside",
|
|
90
|
+
parent,
|
|
91
|
+
". Doing nothing"
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
return correctedTargets;
|
|
98
|
+
}
|
|
99
|
+
__name(correctTargets, "correctTargets");
|
|
100
|
+
function applyAttributeToOthers(originalTargets, parentNode, markerName, controlAttribute) {
|
|
101
|
+
const targets = correctTargets(parentNode, originalTargets);
|
|
102
|
+
const additionalTargets = parentNode.querySelectorAll("[aria-live]");
|
|
103
|
+
for (const target of additionalTargets) {
|
|
104
|
+
targets.add(target);
|
|
105
|
+
}
|
|
106
|
+
let markerCounter = markerMap.get(markerName);
|
|
107
|
+
if (!markerCounter) {
|
|
108
|
+
markerCounter = /* @__PURE__ */ new WeakMap();
|
|
109
|
+
markerMap.set(markerName, markerCounter);
|
|
110
|
+
}
|
|
111
|
+
const hiddenNodes = /* @__PURE__ */ new Set();
|
|
112
|
+
const elementsToKeep = /* @__PURE__ */ new Set();
|
|
113
|
+
const elementsToStop = new Set(targets);
|
|
114
|
+
targets.forEach(keep);
|
|
115
|
+
deep(parentNode);
|
|
116
|
+
elementsToKeep.clear();
|
|
117
|
+
lockCount++;
|
|
118
|
+
return () => {
|
|
119
|
+
for (const node of hiddenNodes) {
|
|
120
|
+
const counterValue = counterMap.get(node) - 1;
|
|
121
|
+
const markerValue = markerCounter.get(node) - 1;
|
|
122
|
+
counterMap.set(node, counterValue);
|
|
123
|
+
markerCounter.set(node, markerValue);
|
|
124
|
+
if (!counterValue) {
|
|
125
|
+
if (!uncontrolledNodes.has(node)) {
|
|
126
|
+
node.removeAttribute(controlAttribute);
|
|
127
|
+
}
|
|
128
|
+
uncontrolledNodes.delete(node);
|
|
129
|
+
}
|
|
130
|
+
if (!markerValue) {
|
|
131
|
+
node.removeAttribute(markerName);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
lockCount--;
|
|
135
|
+
if (!lockCount) {
|
|
136
|
+
counterMap = /* @__PURE__ */ new WeakMap();
|
|
137
|
+
uncontrolledNodes = /* @__PURE__ */ new WeakMap();
|
|
138
|
+
markerMap.clear();
|
|
139
|
+
}
|
|
140
|
+
};
|
|
141
|
+
function keep(el) {
|
|
142
|
+
if (!el || elementsToKeep.has(el)) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
elementsToKeep.add(el);
|
|
146
|
+
keep(el.parentNode);
|
|
147
|
+
}
|
|
148
|
+
__name(keep, "keep");
|
|
149
|
+
function deep(parent) {
|
|
150
|
+
if (!parent || elementsToStop.has(parent)) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
for (const node of parent.children) {
|
|
154
|
+
if (SKIPPED_ELEMENTS.has(node.localName)) {
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (elementsToKeep.has(node)) {
|
|
158
|
+
deep(node);
|
|
159
|
+
} else {
|
|
160
|
+
try {
|
|
161
|
+
const attr = node.getAttribute(controlAttribute);
|
|
162
|
+
const alreadyHidden = controlAttribute === "inert" ? attr !== null : attr !== null && attr !== "false";
|
|
163
|
+
const counterValue = (counterMap.get(node) || 0) + 1;
|
|
164
|
+
const markerValue = (markerCounter?.get(node) || 0) + 1;
|
|
165
|
+
counterMap.set(node, counterValue);
|
|
166
|
+
markerCounter?.set(node, markerValue);
|
|
167
|
+
hiddenNodes.add(node);
|
|
168
|
+
if (counterValue === 1 && alreadyHidden) {
|
|
169
|
+
uncontrolledNodes.set(node, true);
|
|
170
|
+
}
|
|
171
|
+
if (markerValue === 1) {
|
|
172
|
+
node.setAttribute(markerName, "true");
|
|
173
|
+
}
|
|
174
|
+
if (!alreadyHidden) {
|
|
175
|
+
node.setAttribute(controlAttribute, "true");
|
|
176
|
+
}
|
|
177
|
+
} catch (e) {
|
|
178
|
+
if (IS_DEVELOPMENT) {
|
|
179
|
+
console.error("aria-hidden: cannot operate on ", node, e);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
__name(deep, "deep");
|
|
186
|
+
}
|
|
187
|
+
__name(applyAttributeToOthers, "applyAttributeToOthers");
|
|
188
|
+
function hideOthers(originalTargets, parentNode, markerName = "data-aria-hidden") {
|
|
189
|
+
return applyAttributeToOthers(originalTargets, parentNode, markerName, "aria-hidden");
|
|
190
|
+
}
|
|
191
|
+
__name(hideOthers, "hideOthers");
|
|
192
|
+
function inertOthers(originalTargets, parentNode, markerName = "data-inert-ed") {
|
|
193
|
+
return applyAttributeToOthers(originalTargets, parentNode, markerName, "inert");
|
|
194
|
+
}
|
|
195
|
+
__name(inertOthers, "inertOthers");
|
|
196
|
+
function supportsInert() {
|
|
197
|
+
return typeof HTMLElement !== "undefined" && HTMLElement.prototype.hasOwnProperty("inert");
|
|
198
|
+
}
|
|
199
|
+
__name(supportsInert, "supportsInert");
|
|
200
|
+
function suppressOthers(originalTargets, parentNode, markerName = "data-suppressed") {
|
|
201
|
+
return (supportsInert() ? inertOthers : hideOthers)(originalTargets, parentNode, markerName);
|
|
202
|
+
}
|
|
203
|
+
__name(suppressOthers, "suppressOthers");
|
|
53
204
|
export {
|
|
54
205
|
canUseDOM,
|
|
55
206
|
composeEventHandlers,
|
|
56
207
|
getActiveElement,
|
|
57
208
|
getOwnerDocument,
|
|
58
209
|
getOwnerWindow,
|
|
59
|
-
|
|
210
|
+
hideOthers,
|
|
211
|
+
inertOthers,
|
|
212
|
+
isFrame,
|
|
213
|
+
supportsInert,
|
|
214
|
+
suppressOthers
|
|
60
215
|
};
|
|
61
216
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/primitive.tsx"],
|
|
4
|
-
"sourcesContent": ["/*
|
|
5
|
-
"mappings": ";;;;
|
|
3
|
+
"sources": ["../src/can-use-dom.ts", "../src/primitive.tsx", "../src/aria-hidden.ts"],
|
|
4
|
+
"sourcesContent": ["/* oxlint-disable no-restricted-globals */\nexport const canUseDOM = !!(\n typeof window !== 'undefined' &&\n window.document &&\n window.document.createElement\n);\n", "/* oxlint-disable no-restricted-properties */\nimport { canUseDOM } from './can-use-dom';\n\nexport function composeEventHandlers<E extends { defaultPrevented: boolean }>(\n originalEventHandler?: (event: E) => void,\n ourEventHandler?: (event: E) => void,\n { checkForDefaultPrevented = true }: { checkForDefaultPrevented?: boolean | undefined } = {},\n) {\n return function handleEvent(event: E) {\n originalEventHandler?.(event);\n\n if (checkForDefaultPrevented === false || !event || !event.defaultPrevented) {\n return ourEventHandler?.(event);\n }\n };\n}\n\nexport function getOwnerWindow(element: Node | null | undefined) {\n if (!canUseDOM) {\n throw new Error('Cannot access window outside of the DOM');\n }\n // oxlint-disable-next-line no-restricted-globals\n return element?.ownerDocument?.defaultView ?? window;\n}\n\nexport function getOwnerDocument(element: Node | null | undefined) {\n if (!canUseDOM) {\n throw new Error('Cannot access document outside of the DOM');\n }\n // oxlint-disable-next-line no-restricted-globals\n return element?.ownerDocument ?? document;\n}\n\n/**\n * Lifted from https://github.com/ariakit/ariakit/blob/main/packages/ariakit-core/src/utils/dom.ts#L37\n * MIT License, Copyright (c) AriaKit.\n */\nexport function getActiveElement(\n node: Node | null | undefined,\n activeDescendant = false,\n): HTMLElement | null {\n const { activeElement } = getOwnerDocument(node);\n if (!activeElement?.nodeName) {\n // `activeElement` might be an empty object if we're interacting with elements\n // inside of an iframe.\n return null;\n }\n\n if (isFrame(activeElement) && activeElement.contentDocument) {\n return getActiveElement(activeElement.contentDocument.body, activeDescendant);\n }\n\n if (activeDescendant) {\n const id = activeElement.getAttribute('aria-activedescendant');\n if (id) {\n const element = getOwnerDocument(activeElement).getElementById(id);\n if (element) {\n return element;\n }\n }\n }\n\n return activeElement as HTMLElement | null;\n}\n\nexport function isFrame(element: Element): element is HTMLIFrameElement {\n return element.tagName === 'IFRAME';\n}\n", "/**\n * Fork of `aria-hidden`\n *\n * MIT License\n * Copyright (c) 2017 Anton Korzunov\n */\n\nimport { IS_DEVELOPMENT } from '@radix-ui/env-check/is-development';\n\ntype Undo = () => void;\n\ntype DomParentNode = Element | ShadowRoot;\n\nlet counterMap = /* @__PURE__ */ new WeakMap<Element, number>();\nlet uncontrolledNodes = /* @__PURE__ */ new WeakMap<Element, boolean>();\nlet lockCount = /* @__PURE__ */ 0;\nconst markerMap = /* @__PURE__ */ new Map<string, WeakMap<Element, number>>();\n\n// Elements that are never rendered and are not in the accessibility tree, so\n// marking them is wasted work.\n//\n// See https://html.spec.whatwg.org/multipage/rendering.html#hidden-elements\nconst SKIPPED_ELEMENTS = new Set([\n //\n 'link',\n 'meta',\n 'noscript',\n 'script',\n 'style',\n 'template',\n]);\n\nfunction unwrapHost(node: DomParentNode): Element | null {\n return node && ((node as ShadowRoot).host || unwrapHost(node.parentNode as Element));\n}\n\nfunction correctTargets(parent: DomParentNode, targets: Set<Element>): Set<Element> {\n const correctedTargets = new Set<Element>();\n for (const target of targets) {\n if (parent.contains(target)) {\n correctedTargets.add(target);\n } else {\n const correctedTarget = unwrapHost(target);\n if (correctedTarget && parent.contains(correctedTarget)) {\n correctedTargets.add(correctedTarget);\n } else {\n if (IS_DEVELOPMENT) {\n console.error(\n 'aria-hidden',\n target,\n 'in not contained inside',\n parent,\n '. Doing nothing',\n );\n }\n }\n }\n }\n\n return correctedTargets;\n}\n\n/**\n * Marks everything except given node(or nodes) as aria-hidden\n * @param originalTargets - elements to keep on the page\n * @param parentNode - top element\n * @param markerName - a special attribute to mark every node\n * @param controlAttribute - html Attribute to control\n * @return undo command\n */\nfunction applyAttributeToOthers(\n originalTargets: Set<Element>,\n parentNode: DomParentNode,\n markerName: string,\n controlAttribute: string,\n): Undo {\n const targets = correctTargets(parentNode, originalTargets);\n\n // We should not hide aria-live elements\n //\n // See https://github.com/theKashey/aria-hidden/issues/10\n const additionalTargets = parentNode.querySelectorAll('[aria-live]');\n for (const target of additionalTargets) {\n targets.add(target);\n }\n\n let markerCounter = markerMap.get(markerName);\n if (!markerCounter) {\n markerCounter = new WeakMap();\n markerMap.set(markerName, markerCounter);\n }\n\n const hiddenNodes = new Set<Element>();\n const elementsToKeep = new Set<Node>();\n const elementsToStop = new Set<Node>(targets);\n\n targets.forEach(keep);\n deep(parentNode);\n elementsToKeep.clear();\n lockCount++;\n\n return () => {\n for (const node of hiddenNodes) {\n const counterValue = counterMap.get(node)! - 1;\n const markerValue = markerCounter.get(node)! - 1;\n\n counterMap.set(node, counterValue);\n markerCounter.set(node, markerValue);\n\n if (!counterValue) {\n if (!uncontrolledNodes.has(node)) {\n node.removeAttribute(controlAttribute);\n }\n\n uncontrolledNodes.delete(node);\n }\n\n if (!markerValue) {\n node.removeAttribute(markerName);\n }\n }\n\n lockCount--;\n\n if (!lockCount) {\n // clear\n counterMap = new WeakMap();\n uncontrolledNodes = new WeakMap();\n markerMap.clear();\n }\n };\n\n function keep(el: Node | undefined) {\n if (!el || elementsToKeep.has(el)) {\n return;\n }\n\n elementsToKeep.add(el);\n keep(el.parentNode!);\n }\n\n function deep(parent: DomParentNode | null) {\n if (!parent || elementsToStop.has(parent)) {\n return;\n }\n\n for (const node of parent.children) {\n if (SKIPPED_ELEMENTS.has(node.localName)) {\n continue;\n }\n\n if (elementsToKeep.has(node)) {\n deep(node);\n } else {\n try {\n const attr = node.getAttribute(controlAttribute);\n // `inert` is a boolean attribute, so `inert=\"false\"` is still inert\n const alreadyHidden =\n controlAttribute === 'inert' ? attr !== null : attr !== null && attr !== 'false';\n const counterValue = (counterMap.get(node) || 0) + 1;\n const markerValue = (markerCounter?.get(node) || 0) + 1;\n\n counterMap.set(node, counterValue);\n markerCounter?.set(node, markerValue);\n hiddenNodes.add(node);\n\n if (counterValue === 1 && alreadyHidden) {\n uncontrolledNodes.set(node, true);\n }\n\n if (markerValue === 1) {\n node.setAttribute(markerName, 'true');\n }\n\n if (!alreadyHidden) {\n node.setAttribute(controlAttribute, 'true');\n }\n } catch (e) {\n if (IS_DEVELOPMENT) {\n console.error('aria-hidden: cannot operate on ', node, e);\n }\n }\n }\n }\n }\n}\n\n/**\n * Marks everything except given nodes as aria-hidden\n *\n * @param originalTargets - elements to keep on the page\n * @param parentNode - top element\n * @param markerName - a special attribute to mark every node\n * @return undo command\n */\nfunction hideOthers(\n originalTargets: Set<Element>,\n parentNode: DomParentNode,\n markerName = 'data-aria-hidden',\n): Undo {\n return applyAttributeToOthers(originalTargets, parentNode, markerName, 'aria-hidden');\n}\n\n/**\n * Marks everything except given nodes as inert\n *\n * @param originalTargets - elements to keep on the page\n * @param parentNode - top element\n * @param markerName - a special attribute to mark every node\n * @return undo command\n */\nfunction inertOthers(\n originalTargets: Set<Element>,\n parentNode: DomParentNode,\n markerName = 'data-inert-ed',\n): Undo {\n return applyAttributeToOthers(originalTargets, parentNode, markerName, 'inert');\n}\n\nfunction supportsInert(): boolean {\n return typeof HTMLElement !== 'undefined' && HTMLElement.prototype.hasOwnProperty('inert');\n}\n\n/**\n * Automatic function to \"suppress\" DOM elements - _hide_ or _inert_ in the best\n * possible way.\n *\n * @param originalTargets - elements to keep on the page\n * @param parentNode - top element\n * @param markerName - a special attribute to mark every node\n * @return undo command\n */\nfunction suppressOthers(\n originalTargets: Set<Element>,\n parentNode: DomParentNode,\n markerName = 'data-suppressed',\n): Undo {\n return (supportsInert() ? inertOthers : hideOthers)(originalTargets, parentNode, markerName);\n}\n\nexport { hideOthers, inertOthers, supportsInert, suppressOthers };\nexport type { Undo };\n"],
|
|
5
|
+
"mappings": ";;;;AACO,IAAM,YAAY,CAAC,EACxB,OAAO,WAAW,eAClB,OAAO,YACP,OAAO,SAAS;;;ACDX,SAAS,qBACd,sBACA,iBACA,EAAE,2BAA2B,KAAK,IAAwD,CAAC,GAC3F;AACA,SAAO,gCAAS,YAAY,OAAU;AACpC,2BAAuB,KAAK;AAE5B,QAAI,6BAA6B,SAAS,CAAC,SAAS,CAAC,MAAM,kBAAkB;AAC3E,aAAO,kBAAkB,KAAK;AAAA,IAChC;AAAA,EACF,GANO;AAOT;AAZgB;AAcT,SAAS,eAAe,SAAkC;AAC/D,MAAI,CAAC,WAAW;AACd,UAAM,IAAI,MAAM,yCAAyC;AAAA,EAC3D;AAEA,SAAO,SAAS,eAAe,eAAe;AAChD;AANgB;AAQT,SAAS,iBAAiB,SAAkC;AACjE,MAAI,CAAC,WAAW;AACd,UAAM,IAAI,MAAM,2CAA2C;AAAA,EAC7D;AAEA,SAAO,SAAS,iBAAiB;AACnC;AANgB;AAYT,SAAS,iBACd,MACA,mBAAmB,OACC;AACpB,QAAM,EAAE,cAAc,IAAI,iBAAiB,IAAI;AAC/C,MAAI,CAAC,eAAe,UAAU;AAG5B,WAAO;AAAA,EACT;AA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c,YAAY,iBAAiB,YAAY,UAAU;AAC7F;AANS;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@radix-ui/primitive",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.1-rc.1791984254107",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"source": "./src/index.ts",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -15,45 +15,17 @@
|
|
|
15
15
|
"types": "./dist/index.d.ts",
|
|
16
16
|
"default": "./dist/index.js"
|
|
17
17
|
}
|
|
18
|
-
},
|
|
19
|
-
"./is-development": {
|
|
20
|
-
"development": {
|
|
21
|
-
"import": {
|
|
22
|
-
"types": "./dist/internal/is-development.true.d.mts",
|
|
23
|
-
"default": "./dist/internal/is-development.true.mjs"
|
|
24
|
-
},
|
|
25
|
-
"require": {
|
|
26
|
-
"types": "./dist/internal/is-development.true.d.ts",
|
|
27
|
-
"default": "./dist/internal/is-development.true.js"
|
|
28
|
-
}
|
|
29
|
-
},
|
|
30
|
-
"production": {
|
|
31
|
-
"import": {
|
|
32
|
-
"types": "./dist/internal/is-development.false.d.mts",
|
|
33
|
-
"default": "./dist/internal/is-development.false.mjs"
|
|
34
|
-
},
|
|
35
|
-
"require": {
|
|
36
|
-
"types": "./dist/internal/is-development.false.d.ts",
|
|
37
|
-
"default": "./dist/internal/is-development.false.js"
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
"default": {
|
|
41
|
-
"import": {
|
|
42
|
-
"types": "./dist/internal/is-development.false.d.mts",
|
|
43
|
-
"default": "./dist/internal/is-development.false.mjs"
|
|
44
|
-
},
|
|
45
|
-
"require": {
|
|
46
|
-
"types": "./dist/internal/is-development.false.d.ts",
|
|
47
|
-
"default": "./dist/internal/is-development.false.js"
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
18
|
}
|
|
51
19
|
},
|
|
52
20
|
"files": [
|
|
53
21
|
"dist",
|
|
22
|
+
"src",
|
|
54
23
|
"README.md"
|
|
55
24
|
],
|
|
56
25
|
"sideEffects": false,
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@radix-ui/env-check": "0.0.2-rc.1791984254107"
|
|
28
|
+
},
|
|
57
29
|
"devDependencies": {
|
|
58
30
|
"typescript": "^5.9.3",
|
|
59
31
|
"@repo/builder": "0.0.0",
|
|
@@ -73,6 +45,7 @@
|
|
|
73
45
|
"clean": "rm -rf dist",
|
|
74
46
|
"reset": "rm -rf dist node_modules",
|
|
75
47
|
"typecheck": "tsc --noEmit",
|
|
48
|
+
"bench": "node bench/aria-hidden.bench.mts",
|
|
76
49
|
"build": "radix-build"
|
|
77
50
|
},
|
|
78
51
|
"types": "./dist/index.d.ts"
|