@mui/utils 6.0.0-alpha.8 → 6.0.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/CHANGELOG.md +407 -3
  2. package/deepmerge/deepmerge.js +2 -4
  3. package/getDisplayName/getDisplayName.d.ts +1 -1
  4. package/getDisplayName/getDisplayName.js +1 -1
  5. package/index.d.ts +1 -2
  6. package/index.js +2 -3
  7. package/isFocusVisible/index.d.ts +1 -0
  8. package/isFocusVisible/index.js +1 -0
  9. package/isFocusVisible/isFocusVisible.d.ts +4 -0
  10. package/isFocusVisible/isFocusVisible.js +15 -0
  11. package/{scrollLeft → isFocusVisible}/package.json +1 -1
  12. package/modern/deepmerge/deepmerge.js +2 -4
  13. package/modern/getDisplayName/getDisplayName.js +1 -1
  14. package/modern/index.js +2 -3
  15. package/modern/isFocusVisible/index.js +1 -0
  16. package/modern/isFocusVisible/isFocusVisible.js +15 -0
  17. package/modern/useControlled/useControlled.js +3 -1
  18. package/node/deepmerge/deepmerge.js +2 -4
  19. package/node/getDisplayName/getDisplayName.js +1 -1
  20. package/node/index.js +7 -22
  21. package/node/isFocusVisible/index.js +13 -0
  22. package/node/isFocusVisible/isFocusVisible.js +21 -0
  23. package/node/useControlled/useControlled.js +3 -1
  24. package/package.json +3 -3
  25. package/useControlled/useControlled.js +3 -1
  26. package/modern/scrollLeft/index.js +0 -1
  27. package/modern/scrollLeft/scrollLeft.js +0 -70
  28. package/modern/useIsFocusVisible/index.js +0 -2
  29. package/modern/useIsFocusVisible/useIsFocusVisible.js +0 -163
  30. package/node/scrollLeft/index.js +0 -16
  31. package/node/scrollLeft/scrollLeft.js +0 -77
  32. package/node/useIsFocusVisible/index.js +0 -26
  33. package/node/useIsFocusVisible/useIsFocusVisible.js +0 -171
  34. package/scrollLeft/index.d.ts +0 -1
  35. package/scrollLeft/index.js +0 -1
  36. package/scrollLeft/scrollLeft.d.ts +0 -2
  37. package/scrollLeft/scrollLeft.js +0 -70
  38. package/useIsFocusVisible/index.d.ts +0 -2
  39. package/useIsFocusVisible/index.js +0 -2
  40. package/useIsFocusVisible/package.json +0 -6
  41. package/useIsFocusVisible/useIsFocusVisible.d.ts +0 -9
  42. package/useIsFocusVisible/useIsFocusVisible.js +0 -163
@@ -1,171 +0,0 @@
1
- "use strict";
2
- 'use client';
3
-
4
- // based on https://github.com/WICG/focus-visible/blob/v4.1.5/src/focus-visible.js
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.default = useIsFocusVisible;
9
- exports.teardown = teardown;
10
- var React = _interopRequireWildcard(require("react"));
11
- var _useTimeout = require("../useTimeout/useTimeout");
12
- function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
13
- function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
14
- let hadKeyboardEvent = true;
15
- let hadFocusVisibleRecently = false;
16
- const hadFocusVisibleRecentlyTimeout = new _useTimeout.Timeout();
17
- const inputTypesWhitelist = {
18
- text: true,
19
- search: true,
20
- url: true,
21
- tel: true,
22
- email: true,
23
- password: true,
24
- number: true,
25
- date: true,
26
- month: true,
27
- week: true,
28
- time: true,
29
- datetime: true,
30
- 'datetime-local': true
31
- };
32
-
33
- /**
34
- * Computes whether the given element should automatically trigger the
35
- * `focus-visible` class being added, i.e. whether it should always match
36
- * `:focus-visible` when focused.
37
- * @param {Element} node
38
- * @returns {boolean}
39
- */
40
- function focusTriggersKeyboardModality(node) {
41
- const {
42
- type,
43
- tagName
44
- } = node;
45
- if (tagName === 'INPUT' && inputTypesWhitelist[type] && !node.readOnly) {
46
- return true;
47
- }
48
- if (tagName === 'TEXTAREA' && !node.readOnly) {
49
- return true;
50
- }
51
- if (node.isContentEditable) {
52
- return true;
53
- }
54
- return false;
55
- }
56
-
57
- /**
58
- * Keep track of our keyboard modality state with `hadKeyboardEvent`.
59
- * If the most recent user interaction was via the keyboard;
60
- * and the key press did not include a meta, alt/option, or control key;
61
- * then the modality is keyboard. Otherwise, the modality is not keyboard.
62
- * @param {KeyboardEvent} event
63
- */
64
- function handleKeyDown(event) {
65
- if (event.metaKey || event.altKey || event.ctrlKey) {
66
- return;
67
- }
68
- hadKeyboardEvent = true;
69
- }
70
-
71
- /**
72
- * If at any point a user clicks with a pointing device, ensure that we change
73
- * the modality away from keyboard.
74
- * This avoids the situation where a user presses a key on an already focused
75
- * element, and then clicks on a different element, focusing it with a
76
- * pointing device, while we still think we're in keyboard modality.
77
- */
78
- function handlePointerDown() {
79
- hadKeyboardEvent = false;
80
- }
81
- function handleVisibilityChange() {
82
- if (this.visibilityState === 'hidden') {
83
- // If the tab becomes active again, the browser will handle calling focus
84
- // on the element (Safari actually calls it twice).
85
- // If this tab change caused a blur on an element with focus-visible,
86
- // re-apply the class when the user switches back to the tab.
87
- if (hadFocusVisibleRecently) {
88
- hadKeyboardEvent = true;
89
- }
90
- }
91
- }
92
- function prepare(doc) {
93
- doc.addEventListener('keydown', handleKeyDown, true);
94
- doc.addEventListener('mousedown', handlePointerDown, true);
95
- doc.addEventListener('pointerdown', handlePointerDown, true);
96
- doc.addEventListener('touchstart', handlePointerDown, true);
97
- doc.addEventListener('visibilitychange', handleVisibilityChange, true);
98
- }
99
- function teardown(doc) {
100
- doc.removeEventListener('keydown', handleKeyDown, true);
101
- doc.removeEventListener('mousedown', handlePointerDown, true);
102
- doc.removeEventListener('pointerdown', handlePointerDown, true);
103
- doc.removeEventListener('touchstart', handlePointerDown, true);
104
- doc.removeEventListener('visibilitychange', handleVisibilityChange, true);
105
- }
106
- function isFocusVisible(event) {
107
- const {
108
- target
109
- } = event;
110
- try {
111
- return target.matches(':focus-visible');
112
- } catch (error) {
113
- // Browsers not implementing :focus-visible will throw a SyntaxError.
114
- // We use our own heuristic for those browsers.
115
- // Rethrow might be better if it's not the expected error but do we really
116
- // want to crash if focus-visible malfunctioned?
117
- }
118
-
119
- // No need for validFocusTarget check. The user does that by attaching it to
120
- // focusable events only.
121
- return hadKeyboardEvent || focusTriggersKeyboardModality(target);
122
- }
123
- function useIsFocusVisible() {
124
- const ref = React.useCallback(node => {
125
- if (node != null) {
126
- prepare(node.ownerDocument);
127
- }
128
- }, []);
129
- const isFocusVisibleRef = React.useRef(false);
130
-
131
- /**
132
- * Should be called if a blur event is fired
133
- */
134
- function handleBlurVisible() {
135
- // checking against potential state variable does not suffice if we focus and blur synchronously.
136
- // React wouldn't have time to trigger a re-render so `focusVisible` would be stale.
137
- // Ideally we would adjust `isFocusVisible(event)` to look at `relatedTarget` for blur events.
138
- // This doesn't work in IE11 due to https://github.com/facebook/react/issues/3751
139
- // TODO: check again if React releases their internal changes to focus event handling (https://github.com/facebook/react/pull/19186).
140
- if (isFocusVisibleRef.current) {
141
- // To detect a tab/window switch, we look for a blur event followed
142
- // rapidly by a visibility change.
143
- // If we don't see a visibility change within 100ms, it's probably a
144
- // regular focus change.
145
- hadFocusVisibleRecently = true;
146
- hadFocusVisibleRecentlyTimeout.start(100, () => {
147
- hadFocusVisibleRecently = false;
148
- });
149
- isFocusVisibleRef.current = false;
150
- return true;
151
- }
152
- return false;
153
- }
154
-
155
- /**
156
- * Should be called if a blur event is fired
157
- */
158
- function handleFocusVisible(event) {
159
- if (isFocusVisible(event)) {
160
- isFocusVisibleRef.current = true;
161
- return true;
162
- }
163
- return false;
164
- }
165
- return {
166
- isFocusVisibleRef,
167
- onFocus: handleFocusVisible,
168
- onBlur: handleBlurVisible,
169
- ref
170
- };
171
- }
@@ -1 +0,0 @@
1
- export * from './scrollLeft';
@@ -1 +0,0 @@
1
- export * from './scrollLeft';
@@ -1,2 +0,0 @@
1
- export function detectScrollType(): string;
2
- export function getNormalizedScrollLeft(element: HTMLElement, direction: string): number;
@@ -1,70 +0,0 @@
1
- // Source from https://github.com/alitaheri/normalize-scroll-left
2
- let cachedType;
3
-
4
- /**
5
- * Based on the jquery plugin https://github.com/othree/jquery.rtl-scroll-type
6
- *
7
- * Types of scrollLeft, assuming scrollWidth=100 and direction is rtl.
8
- *
9
- * Type | <- Most Left | Most Right -> | Initial
10
- * ---------------- | ------------ | ------------- | -------
11
- * default | 0 | 100 | 100
12
- * negative (spec*) | -100 | 0 | 0
13
- * reverse | 100 | 0 | 0
14
- *
15
- * Edge 85: default
16
- * Safari 14: negative
17
- * Chrome 85: negative
18
- * Firefox 81: negative
19
- * IE11: reverse
20
- *
21
- * spec* https://drafts.csswg.org/cssom-view/#dom-window-scroll
22
- */
23
- export function detectScrollType() {
24
- if (cachedType) {
25
- return cachedType;
26
- }
27
- const dummy = document.createElement('div');
28
- const container = document.createElement('div');
29
- container.style.width = '10px';
30
- container.style.height = '1px';
31
- dummy.appendChild(container);
32
- dummy.dir = 'rtl';
33
- dummy.style.fontSize = '14px';
34
- dummy.style.width = '4px';
35
- dummy.style.height = '1px';
36
- dummy.style.position = 'absolute';
37
- dummy.style.top = '-1000px';
38
- dummy.style.overflow = 'scroll';
39
- document.body.appendChild(dummy);
40
- cachedType = 'reverse';
41
- if (dummy.scrollLeft > 0) {
42
- cachedType = 'default';
43
- } else {
44
- dummy.scrollLeft = 1;
45
- if (dummy.scrollLeft === 0) {
46
- cachedType = 'negative';
47
- }
48
- }
49
- document.body.removeChild(dummy);
50
- return cachedType;
51
- }
52
-
53
- // Based on https://stackoverflow.com/a/24394376
54
- export function getNormalizedScrollLeft(element, direction) {
55
- const scrollLeft = element.scrollLeft;
56
-
57
- // Perform the calculations only when direction is rtl to avoid messing up the ltr behavior
58
- if (direction !== 'rtl') {
59
- return scrollLeft;
60
- }
61
- const type = detectScrollType();
62
- switch (type) {
63
- case 'negative':
64
- return element.scrollWidth - element.clientWidth + scrollLeft;
65
- case 'reverse':
66
- return element.scrollWidth - element.clientWidth - scrollLeft;
67
- default:
68
- return scrollLeft;
69
- }
70
- }
@@ -1,2 +0,0 @@
1
- export { default } from './useIsFocusVisible';
2
- export * from './useIsFocusVisible';
@@ -1,2 +0,0 @@
1
- export { default } from './useIsFocusVisible';
2
- export * from './useIsFocusVisible';
@@ -1,6 +0,0 @@
1
- {
2
- "sideEffects": false,
3
- "module": "./index.js",
4
- "main": "../node/useIsFocusVisible/index.js",
5
- "types": "./index.d.ts"
6
- }
@@ -1,9 +0,0 @@
1
- import * as React from 'react';
2
- export declare function teardown(doc: Document): void;
3
- export interface UseIsFocusVisibleResult {
4
- isFocusVisibleRef: React.MutableRefObject<boolean>;
5
- onBlur: (event: React.FocusEvent<any>) => void;
6
- onFocus: (event: React.FocusEvent<any>) => void;
7
- ref: React.RefCallback<Element>;
8
- }
9
- export default function useIsFocusVisible(): UseIsFocusVisibleResult;
@@ -1,163 +0,0 @@
1
- 'use client';
2
-
3
- // based on https://github.com/WICG/focus-visible/blob/v4.1.5/src/focus-visible.js
4
- import * as React from 'react';
5
- import { Timeout } from '../useTimeout/useTimeout';
6
- let hadKeyboardEvent = true;
7
- let hadFocusVisibleRecently = false;
8
- const hadFocusVisibleRecentlyTimeout = new Timeout();
9
- const inputTypesWhitelist = {
10
- text: true,
11
- search: true,
12
- url: true,
13
- tel: true,
14
- email: true,
15
- password: true,
16
- number: true,
17
- date: true,
18
- month: true,
19
- week: true,
20
- time: true,
21
- datetime: true,
22
- 'datetime-local': true
23
- };
24
-
25
- /**
26
- * Computes whether the given element should automatically trigger the
27
- * `focus-visible` class being added, i.e. whether it should always match
28
- * `:focus-visible` when focused.
29
- * @param {Element} node
30
- * @returns {boolean}
31
- */
32
- function focusTriggersKeyboardModality(node) {
33
- const {
34
- type,
35
- tagName
36
- } = node;
37
- if (tagName === 'INPUT' && inputTypesWhitelist[type] && !node.readOnly) {
38
- return true;
39
- }
40
- if (tagName === 'TEXTAREA' && !node.readOnly) {
41
- return true;
42
- }
43
- if (node.isContentEditable) {
44
- return true;
45
- }
46
- return false;
47
- }
48
-
49
- /**
50
- * Keep track of our keyboard modality state with `hadKeyboardEvent`.
51
- * If the most recent user interaction was via the keyboard;
52
- * and the key press did not include a meta, alt/option, or control key;
53
- * then the modality is keyboard. Otherwise, the modality is not keyboard.
54
- * @param {KeyboardEvent} event
55
- */
56
- function handleKeyDown(event) {
57
- if (event.metaKey || event.altKey || event.ctrlKey) {
58
- return;
59
- }
60
- hadKeyboardEvent = true;
61
- }
62
-
63
- /**
64
- * If at any point a user clicks with a pointing device, ensure that we change
65
- * the modality away from keyboard.
66
- * This avoids the situation where a user presses a key on an already focused
67
- * element, and then clicks on a different element, focusing it with a
68
- * pointing device, while we still think we're in keyboard modality.
69
- */
70
- function handlePointerDown() {
71
- hadKeyboardEvent = false;
72
- }
73
- function handleVisibilityChange() {
74
- if (this.visibilityState === 'hidden') {
75
- // If the tab becomes active again, the browser will handle calling focus
76
- // on the element (Safari actually calls it twice).
77
- // If this tab change caused a blur on an element with focus-visible,
78
- // re-apply the class when the user switches back to the tab.
79
- if (hadFocusVisibleRecently) {
80
- hadKeyboardEvent = true;
81
- }
82
- }
83
- }
84
- function prepare(doc) {
85
- doc.addEventListener('keydown', handleKeyDown, true);
86
- doc.addEventListener('mousedown', handlePointerDown, true);
87
- doc.addEventListener('pointerdown', handlePointerDown, true);
88
- doc.addEventListener('touchstart', handlePointerDown, true);
89
- doc.addEventListener('visibilitychange', handleVisibilityChange, true);
90
- }
91
- export function teardown(doc) {
92
- doc.removeEventListener('keydown', handleKeyDown, true);
93
- doc.removeEventListener('mousedown', handlePointerDown, true);
94
- doc.removeEventListener('pointerdown', handlePointerDown, true);
95
- doc.removeEventListener('touchstart', handlePointerDown, true);
96
- doc.removeEventListener('visibilitychange', handleVisibilityChange, true);
97
- }
98
- function isFocusVisible(event) {
99
- const {
100
- target
101
- } = event;
102
- try {
103
- return target.matches(':focus-visible');
104
- } catch (error) {
105
- // Browsers not implementing :focus-visible will throw a SyntaxError.
106
- // We use our own heuristic for those browsers.
107
- // Rethrow might be better if it's not the expected error but do we really
108
- // want to crash if focus-visible malfunctioned?
109
- }
110
-
111
- // No need for validFocusTarget check. The user does that by attaching it to
112
- // focusable events only.
113
- return hadKeyboardEvent || focusTriggersKeyboardModality(target);
114
- }
115
- export default function useIsFocusVisible() {
116
- const ref = React.useCallback(node => {
117
- if (node != null) {
118
- prepare(node.ownerDocument);
119
- }
120
- }, []);
121
- const isFocusVisibleRef = React.useRef(false);
122
-
123
- /**
124
- * Should be called if a blur event is fired
125
- */
126
- function handleBlurVisible() {
127
- // checking against potential state variable does not suffice if we focus and blur synchronously.
128
- // React wouldn't have time to trigger a re-render so `focusVisible` would be stale.
129
- // Ideally we would adjust `isFocusVisible(event)` to look at `relatedTarget` for blur events.
130
- // This doesn't work in IE11 due to https://github.com/facebook/react/issues/3751
131
- // TODO: check again if React releases their internal changes to focus event handling (https://github.com/facebook/react/pull/19186).
132
- if (isFocusVisibleRef.current) {
133
- // To detect a tab/window switch, we look for a blur event followed
134
- // rapidly by a visibility change.
135
- // If we don't see a visibility change within 100ms, it's probably a
136
- // regular focus change.
137
- hadFocusVisibleRecently = true;
138
- hadFocusVisibleRecentlyTimeout.start(100, () => {
139
- hadFocusVisibleRecently = false;
140
- });
141
- isFocusVisibleRef.current = false;
142
- return true;
143
- }
144
- return false;
145
- }
146
-
147
- /**
148
- * Should be called if a blur event is fired
149
- */
150
- function handleFocusVisible(event) {
151
- if (isFocusVisible(event)) {
152
- isFocusVisibleRef.current = true;
153
- return true;
154
- }
155
- return false;
156
- }
157
- return {
158
- isFocusVisibleRef,
159
- onFocus: handleFocusVisible,
160
- onBlur: handleBlurVisible,
161
- ref
162
- };
163
- }