datocms-react-ui 0.3.27 → 0.3.28

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 (89) hide show
  1. package/package.json +3 -2
  2. package/src/Button/index.tsx +173 -0
  3. package/src/Button/styles.module.css +149 -0
  4. package/src/Button/styles.module.css.json +1 -0
  5. package/src/ButtonGroup/Button/index.tsx +40 -0
  6. package/src/ButtonGroup/Button/styles.module.css +72 -0
  7. package/src/ButtonGroup/Button/styles.module.css.json +1 -0
  8. package/src/ButtonGroup/Group/index.tsx +31 -0
  9. package/src/ButtonGroup/Group/styles.module.css +6 -0
  10. package/src/ButtonGroup/Group/styles.module.css.json +1 -0
  11. package/src/ButtonGroup/index.ts +4 -0
  12. package/src/Canvas/index.tsx +556 -0
  13. package/src/Canvas/styles.module.css +75 -0
  14. package/src/Canvas/styles.module.css.json +1 -0
  15. package/src/ContextInspector/index.tsx +316 -0
  16. package/src/ContextInspector/styles.module.css +90 -0
  17. package/src/ContextInspector/styles.module.css.json +1 -0
  18. package/src/Dropdown/Dropdown.tsx +171 -0
  19. package/src/Dropdown/DropdownContext.tsx +10 -0
  20. package/src/Dropdown/Group.tsx +16 -0
  21. package/src/Dropdown/Menu.tsx +351 -0
  22. package/src/Dropdown/MenuContext.tsx +18 -0
  23. package/src/Dropdown/Option.tsx +148 -0
  24. package/src/Dropdown/OptionAction.tsx +42 -0
  25. package/src/Dropdown/Portal.tsx +46 -0
  26. package/src/Dropdown/Separator.tsx +13 -0
  27. package/src/Dropdown/Text.tsx +8 -0
  28. package/src/Dropdown/index.tsx +26 -0
  29. package/src/Dropdown/styles.module.css +331 -0
  30. package/src/Dropdown/styles.module.css.json +1 -0
  31. package/src/FieldError/index.tsx +10 -0
  32. package/src/FieldError/styles.module.css +6 -0
  33. package/src/FieldError/styles.module.css.json +1 -0
  34. package/src/FieldGroup/index.tsx +25 -0
  35. package/src/FieldGroup/styles.module.css +12 -0
  36. package/src/FieldGroup/styles.module.css.json +1 -0
  37. package/src/FieldHint/index.tsx +10 -0
  38. package/src/FieldHint/styles.module.css +6 -0
  39. package/src/FieldHint/styles.module.css.json +1 -0
  40. package/src/Form/index.tsx +145 -0
  41. package/src/Form/styles.module.css +19 -0
  42. package/src/Form/styles.module.css.json +1 -0
  43. package/src/FormLabel/index.tsx +36 -0
  44. package/src/FormLabel/styles.module.css +31 -0
  45. package/src/FormLabel/styles.module.css.json +1 -0
  46. package/src/Section/index.tsx +104 -0
  47. package/src/Section/styles.module.css +100 -0
  48. package/src/Section/styles.module.css.json +1 -0
  49. package/src/SelectField/index.tsx +244 -0
  50. package/src/SelectInput/index.tsx +233 -0
  51. package/src/SidebarPanel/index.tsx +110 -0
  52. package/src/SidebarPanel/styles.module.css +49 -0
  53. package/src/SidebarPanel/styles.module.css.json +1 -0
  54. package/src/Spinner/index.tsx +68 -0
  55. package/src/Spinner/styles.module.css +31 -0
  56. package/src/Spinner/styles.module.css.json +1 -0
  57. package/src/SwitchField/index.tsx +67 -0
  58. package/src/SwitchField/styles.module.css +25 -0
  59. package/src/SwitchField/styles.module.css.json +1 -0
  60. package/src/SwitchInput/index.tsx +74 -0
  61. package/src/SwitchInput/styles.module.css +100 -0
  62. package/src/SwitchInput/styles.module.css.json +1 -0
  63. package/src/TextField/index.tsx +58 -0
  64. package/src/TextField/styles.module.css +0 -0
  65. package/src/TextField/styles.module.css.json +1 -0
  66. package/src/TextInput/index.tsx +73 -0
  67. package/src/TextInput/styles.module.css +52 -0
  68. package/src/TextInput/styles.module.css.json +1 -0
  69. package/src/Toolbar/Button/index.tsx +32 -0
  70. package/src/Toolbar/Button/styles.module.css +43 -0
  71. package/src/Toolbar/Button/styles.module.css.json +1 -0
  72. package/src/Toolbar/Stack/index.tsx +33 -0
  73. package/src/Toolbar/Stack/styles.module.css +18 -0
  74. package/src/Toolbar/Stack/styles.module.css.json +1 -0
  75. package/src/Toolbar/Title/index.tsx +17 -0
  76. package/src/Toolbar/Title/styles.module.css +12 -0
  77. package/src/Toolbar/Title/styles.module.css.json +1 -0
  78. package/src/Toolbar/Toolbar/index.tsx +112 -0
  79. package/src/Toolbar/Toolbar/styles.module.css +15 -0
  80. package/src/Toolbar/Toolbar/styles.module.css.json +1 -0
  81. package/src/Toolbar/index.ts +8 -0
  82. package/src/base.css +89 -0
  83. package/src/generateStyleFromCtx/index.ts +25 -0
  84. package/src/global.css +23 -0
  85. package/src/icons.tsx +108 -0
  86. package/src/index.ts +23 -0
  87. package/src/mergeRefs/index.ts +8 -0
  88. package/src/useClickOutside/index.ts +30 -0
  89. package/src/useMediaQuery/index.ts +185 -0
@@ -0,0 +1,185 @@
1
+ import React from 'react';
2
+
3
+ const list: Record<
4
+ string,
5
+ {
6
+ mql: MediaQueryList;
7
+ notifiers: Array<(value: Record<string, never>) => void>;
8
+ listener: (mql: MediaQueryListEvent) => void;
9
+ }
10
+ > = {};
11
+
12
+ export function useMediaQuery(media: string): MediaQueryList {
13
+ const mediaQueryList = React.useMemo(() => {
14
+ // We have an existing mediaQueryList for media.
15
+ if (list[media]) {
16
+ return list[media].mql;
17
+ }
18
+
19
+ if (typeof window !== 'undefined') {
20
+ return matchMedia(media);
21
+ }
22
+
23
+ // Server: use a fallback.
24
+ return { matches: true, media } as MediaQueryList;
25
+ }, [list, media]);
26
+
27
+ // Since the mediaQueryList is shared we use state to trigger re-renders.
28
+ // This is done with update({}) (a new object instance will force a re-rendering).
29
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
30
+ const [_, update] = React.useState<Record<string, never>>({});
31
+
32
+ React.useLayoutEffect(() => {
33
+ if (list[media]) {
34
+ list[media].notifiers.push(update);
35
+ } else {
36
+ list[media] = {
37
+ mql: mediaQueryList,
38
+ notifiers: [update],
39
+ listener: (mql) => {
40
+ const match = list[mql.media];
41
+ if (match) {
42
+ match.notifiers.forEach((updateFn) => updateFn({}));
43
+ }
44
+ },
45
+ };
46
+ list[media].mql.addEventListener('change', list[media].listener);
47
+ }
48
+ return () => {
49
+ const notifierIndex = list[media].notifiers.indexOf(update);
50
+ if (notifierIndex > -1) {
51
+ // Remove `update`.
52
+ list[media].notifiers.splice(notifierIndex, 1);
53
+ }
54
+ if (list[media].notifiers.length === 0) {
55
+ list[media].mql.removeEventListener('change', list[media].listener);
56
+ delete list[media];
57
+ }
58
+ };
59
+ }, [list, media, mediaQueryList]);
60
+
61
+ return mediaQueryList;
62
+ }
63
+
64
+ export type MediaQueryProps = {
65
+ media: string;
66
+ children: (mql: MediaQueryList) => JSX.Element;
67
+ };
68
+
69
+ export function MediaQuery({
70
+ media,
71
+ children,
72
+ }: MediaQueryProps): JSX.Element | null {
73
+ const mql = useMediaQuery(media);
74
+ return typeof children === 'function' ? children(mql) : null;
75
+ }
76
+
77
+ const DOM_LAYOUT_HANDLER_NAME = '__reactLayoutHandler';
78
+
79
+ const supportsBorderBox = (() => {
80
+ if (typeof document === 'undefined') {
81
+ return false;
82
+ }
83
+ const e = document.body.appendChild(document.createElement('div'));
84
+ // eslint-disable-next-line @typescript-eslint/no-empty-function
85
+ const observer = new ResizeObserver(() => {});
86
+ try {
87
+ observer.observe(e, { box: 'border-box' });
88
+ observer.unobserve(e);
89
+ return true;
90
+ } catch (error) {
91
+ return false;
92
+ } finally {
93
+ document.body.removeChild(e);
94
+ }
95
+ })();
96
+
97
+ let resizeObserver: ResizeObserver | null = null;
98
+
99
+ function getResizeObserver(): ResizeObserver | undefined {
100
+ if (resizeObserver) {
101
+ return resizeObserver;
102
+ }
103
+
104
+ if (typeof ResizeObserver === 'undefined') {
105
+ return;
106
+ }
107
+
108
+ resizeObserver = new ResizeObserver((entries) => {
109
+ entries.forEach((entry) => {
110
+ const node = entry.target;
111
+
112
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
113
+ const onLayout = (node as any)[DOM_LAYOUT_HANDLER_NAME] as
114
+ | React.Dispatch<React.SetStateAction<DOMRect>>
115
+ | undefined;
116
+
117
+ if (typeof onLayout !== 'function') {
118
+ return;
119
+ }
120
+
121
+ if (supportsBorderBox && entry.borderBoxSize) {
122
+ const boxSize = entry.borderBoxSize[0] || entry.borderBoxSize;
123
+ onLayout((prevRect) => {
124
+ if (
125
+ prevRect.width === boxSize.inlineSize &&
126
+ prevRect.height === boxSize.blockSize
127
+ ) {
128
+ return prevRect;
129
+ }
130
+ return new DOMRect(0, 0, boxSize.inlineSize, boxSize.blockSize);
131
+ });
132
+ return;
133
+ }
134
+
135
+ const elRect = entry.target.getBoundingClientRect();
136
+
137
+ onLayout((prevRect) => {
138
+ if (
139
+ prevRect.width === elRect.width &&
140
+ prevRect.height === elRect.height
141
+ ) {
142
+ return prevRect;
143
+ }
144
+ return new DOMRect(0, 0, elRect.width, elRect.height);
145
+ });
146
+ });
147
+ });
148
+
149
+ return resizeObserver;
150
+ }
151
+
152
+ export function useElementLayout(
153
+ ref: React.MutableRefObject<Element> | React.RefObject<Element>,
154
+ ): DOMRect {
155
+ const observer = getResizeObserver();
156
+ const [rect, setRect] = React.useState(new DOMRect());
157
+
158
+ React.useLayoutEffect(() => {
159
+ const node = ref.current;
160
+
161
+ if (node) {
162
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
163
+ (node as any)[DOM_LAYOUT_HANDLER_NAME] = setRect;
164
+ }
165
+
166
+ if (node && observer) {
167
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
168
+ if (typeof (node as any)[DOM_LAYOUT_HANDLER_NAME] === 'function') {
169
+ observer.observe(
170
+ node,
171
+ supportsBorderBox ? { box: 'border-box' } : undefined,
172
+ );
173
+ } else {
174
+ observer.unobserve(node);
175
+ }
176
+ }
177
+ return () => {
178
+ if (node != null && observer != null) {
179
+ observer.unobserve(node);
180
+ }
181
+ };
182
+ }, [ref, observer]);
183
+
184
+ return rect;
185
+ }