@blockle/blocks 0.12.2 → 0.14.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.
@@ -88,20 +88,6 @@ const classnames = (...args) => {
88
88
  const className = args.filter((arg) => arg && typeof arg === "string").join(" ");
89
89
  return className || void 0;
90
90
  };
91
- function setRef(ref, value) {
92
- if (typeof ref === "function") {
93
- ref(value);
94
- } else if (ref !== null && ref !== void 0) {
95
- ref.current = value;
96
- }
97
- }
98
- function composeRefs(...refs) {
99
- return (node) => {
100
- for (const ref of refs) {
101
- setRef(ref, node);
102
- }
103
- };
104
- }
105
91
  function mergeProps(slotProps, childProps) {
106
92
  const overrideProps = {};
107
93
  for (const propName in childProps) {
@@ -122,82 +108,81 @@ function mergeProps(slotProps, childProps) {
122
108
  } else if (propName === "style") {
123
109
  overrideProps[propName] = { ...slotPropValue, ...childPropValue };
124
110
  } else if (propName === "className") {
125
- overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(" ");
111
+ overrideProps[propName] = classnames(slotPropValue, childPropValue);
126
112
  } else {
127
113
  overrideProps[propName] = childPropValue;
128
114
  }
129
115
  }
130
116
  return { ...slotProps, ...overrideProps };
131
117
  }
132
- function createSlottable(defaultElement) {
133
- function Slottable2(props, ref) {
134
- const { asChild, children, ...slotProps } = props;
135
- const Component = defaultElement;
118
+ function setRef(ref, value) {
119
+ if (typeof ref === "function") {
120
+ ref(value);
121
+ } else if (ref !== null && ref !== void 0) {
122
+ ref.current = value;
123
+ }
124
+ }
125
+ function composeRefs(...refs) {
126
+ return (node) => {
127
+ for (const ref of refs) {
128
+ setRef(ref, node);
129
+ }
130
+ };
131
+ }
132
+ function createAsChildTemplate(defaultElement) {
133
+ const Tag = defaultElement;
134
+ const Template2 = react.forwardRef(function Template22({ asChild, children, ...rootProps }, ref) {
136
135
  if (!asChild) {
137
- return /* @__PURE__ */ jsxRuntime.jsx(Component, { ref, ...slotProps, children });
136
+ const tagProps = { ref, ...rootProps };
137
+ return /* @__PURE__ */ jsxRuntime.jsx(Tag, { ...tagProps, children });
138
138
  }
139
139
  const childrenArray = react.Children.toArray(children);
140
- const slot = childrenArray.find((child) => {
140
+ const slotIndex = childrenArray.findIndex((child) => {
141
141
  if (!react.isValidElement(child)) {
142
142
  return false;
143
143
  }
144
- return child.type === Slot;
144
+ return child.type === Slot$1;
145
145
  });
146
+ const slot = childrenArray[slotIndex];
146
147
  if (!slot) {
147
- const Slot2 = childrenArray[0];
148
- if (!react.isValidElement(childrenArray[0])) {
149
- if (process.env.NODE_ENV === "development") {
150
- console.warn("Slottable: First child is not a valid React element");
151
- }
152
- return null;
148
+ if (process.env.NODE_ENV === "development") {
149
+ console.error("Template: No Slot provided");
153
150
  }
154
- if (!react.isValidElement(Slot2)) {
155
- return null;
156
- }
157
- return react.cloneElement(
158
- Slot2,
159
- {
160
- ...mergeProps(slotProps, Slot2.props),
161
- ref: composeRefs(ref, Slot2.ref)
162
- },
163
- Slot2.props.children
164
- );
151
+ return null;
165
152
  }
166
153
  if (!react.isValidElement(slot) || !react.isValidElement(slot.props.children)) {
167
154
  return null;
168
155
  }
169
- const newChildren = childrenArray.map((child) => {
170
- if (!react.isValidElement(child)) {
171
- return child;
172
- }
173
- if (child.type === Slot) {
174
- return slot.props.children.props.children;
175
- }
176
- return child;
177
- });
156
+ const nextChildren = [...childrenArray];
157
+ nextChildren[slotIndex] = slot.props.children.props.children;
178
158
  return react.cloneElement(
179
159
  slot.props.children,
180
160
  {
181
- ...mergeProps(slotProps, slot.props),
161
+ ...mergeProps(rootProps, slot.props.children.props),
182
162
  ref: composeRefs(ref, slot.props.children.ref)
183
163
  },
184
- newChildren
164
+ nextChildren
185
165
  );
186
- }
187
- return react.forwardRef(Slottable2);
166
+ });
167
+ return {
168
+ Template: Template2,
169
+ Slot: Slot$1
170
+ };
188
171
  }
189
- const Slot = ({ children }) => children;
190
- const Slottable = createSlottable("div");
172
+ const Slot$1 = ({ children }) => {
173
+ return children;
174
+ };
175
+ const { Template, Slot } = createAsChildTemplate("div");
191
176
  const Box = react.forwardRef(function Box2({ asChild, className, children, ...restProps }, ref) {
192
177
  const [atomsProps, otherProps] = styles_lib_utils_atomProps_cjs.getAtomsAndProps(restProps);
193
178
  return /* @__PURE__ */ jsxRuntime.jsx(
194
- Slottable,
179
+ Template,
195
180
  {
196
181
  ref,
197
182
  asChild,
198
183
  className: classnames(className, styles_lib_css_atoms_sprinkles_css_cjs.atoms(atomsProps)),
199
184
  ...otherProps,
200
- children
185
+ children: /* @__PURE__ */ jsxRuntime.jsx(Slot, { children })
201
186
  }
202
187
  );
203
188
  });
@@ -218,10 +203,9 @@ const Divider = ({ className, color, ...restProps }) => {
218
203
  exports.BlocksProviderContext = BlocksProviderContext;
219
204
  exports.Box = Box;
220
205
  exports.Divider = Divider;
221
- exports.Slot = Slot;
222
206
  exports.classnames = classnames;
223
207
  exports.composeRefs = composeRefs;
224
- exports.createSlottable = createSlottable;
208
+ exports.createAsChildTemplate = createAsChildTemplate;
225
209
  exports.useComponentStyleDefaultProps = useComponentStyleDefaultProps;
226
210
  exports.useComponentStyles = useComponentStyles;
227
211
  exports.useTheme = useTheme;
@@ -87,20 +87,6 @@ const classnames = (...args) => {
87
87
  const className = args.filter((arg) => arg && typeof arg === "string").join(" ");
88
88
  return className || void 0;
89
89
  };
90
- function setRef(ref, value) {
91
- if (typeof ref === "function") {
92
- ref(value);
93
- } else if (ref !== null && ref !== void 0) {
94
- ref.current = value;
95
- }
96
- }
97
- function composeRefs(...refs) {
98
- return (node) => {
99
- for (const ref of refs) {
100
- setRef(ref, node);
101
- }
102
- };
103
- }
104
90
  function mergeProps(slotProps, childProps) {
105
91
  const overrideProps = {};
106
92
  for (const propName in childProps) {
@@ -121,82 +107,81 @@ function mergeProps(slotProps, childProps) {
121
107
  } else if (propName === "style") {
122
108
  overrideProps[propName] = { ...slotPropValue, ...childPropValue };
123
109
  } else if (propName === "className") {
124
- overrideProps[propName] = [slotPropValue, childPropValue].filter(Boolean).join(" ");
110
+ overrideProps[propName] = classnames(slotPropValue, childPropValue);
125
111
  } else {
126
112
  overrideProps[propName] = childPropValue;
127
113
  }
128
114
  }
129
115
  return { ...slotProps, ...overrideProps };
130
116
  }
131
- function createSlottable(defaultElement) {
132
- function Slottable2(props, ref) {
133
- const { asChild, children, ...slotProps } = props;
134
- const Component = defaultElement;
117
+ function setRef(ref, value) {
118
+ if (typeof ref === "function") {
119
+ ref(value);
120
+ } else if (ref !== null && ref !== void 0) {
121
+ ref.current = value;
122
+ }
123
+ }
124
+ function composeRefs(...refs) {
125
+ return (node) => {
126
+ for (const ref of refs) {
127
+ setRef(ref, node);
128
+ }
129
+ };
130
+ }
131
+ function createAsChildTemplate(defaultElement) {
132
+ const Tag = defaultElement;
133
+ const Template2 = forwardRef(function Template22({ asChild, children, ...rootProps }, ref) {
135
134
  if (!asChild) {
136
- return /* @__PURE__ */ jsx(Component, { ref, ...slotProps, children });
135
+ const tagProps = { ref, ...rootProps };
136
+ return /* @__PURE__ */ jsx(Tag, { ...tagProps, children });
137
137
  }
138
138
  const childrenArray = Children.toArray(children);
139
- const slot = childrenArray.find((child) => {
139
+ const slotIndex = childrenArray.findIndex((child) => {
140
140
  if (!isValidElement(child)) {
141
141
  return false;
142
142
  }
143
- return child.type === Slot;
143
+ return child.type === Slot$1;
144
144
  });
145
+ const slot = childrenArray[slotIndex];
145
146
  if (!slot) {
146
- const Slot2 = childrenArray[0];
147
- if (!isValidElement(childrenArray[0])) {
148
- if (process.env.NODE_ENV === "development") {
149
- console.warn("Slottable: First child is not a valid React element");
150
- }
151
- return null;
147
+ if (process.env.NODE_ENV === "development") {
148
+ console.error("Template: No Slot provided");
152
149
  }
153
- if (!isValidElement(Slot2)) {
154
- return null;
155
- }
156
- return cloneElement(
157
- Slot2,
158
- {
159
- ...mergeProps(slotProps, Slot2.props),
160
- ref: composeRefs(ref, Slot2.ref)
161
- },
162
- Slot2.props.children
163
- );
150
+ return null;
164
151
  }
165
152
  if (!isValidElement(slot) || !isValidElement(slot.props.children)) {
166
153
  return null;
167
154
  }
168
- const newChildren = childrenArray.map((child) => {
169
- if (!isValidElement(child)) {
170
- return child;
171
- }
172
- if (child.type === Slot) {
173
- return slot.props.children.props.children;
174
- }
175
- return child;
176
- });
155
+ const nextChildren = [...childrenArray];
156
+ nextChildren[slotIndex] = slot.props.children.props.children;
177
157
  return cloneElement(
178
158
  slot.props.children,
179
159
  {
180
- ...mergeProps(slotProps, slot.props),
160
+ ...mergeProps(rootProps, slot.props.children.props),
181
161
  ref: composeRefs(ref, slot.props.children.ref)
182
162
  },
183
- newChildren
163
+ nextChildren
184
164
  );
185
- }
186
- return forwardRef(Slottable2);
165
+ });
166
+ return {
167
+ Template: Template2,
168
+ Slot: Slot$1
169
+ };
187
170
  }
188
- const Slot = ({ children }) => children;
189
- const Slottable = createSlottable("div");
171
+ const Slot$1 = ({ children }) => {
172
+ return children;
173
+ };
174
+ const { Template, Slot } = createAsChildTemplate("div");
190
175
  const Box = forwardRef(function Box2({ asChild, className, children, ...restProps }, ref) {
191
176
  const [atomsProps, otherProps] = getAtomsAndProps(restProps);
192
177
  return /* @__PURE__ */ jsx(
193
- Slottable,
178
+ Template,
194
179
  {
195
180
  ref,
196
181
  asChild,
197
182
  className: classnames(className, atoms(atomsProps)),
198
183
  ...otherProps,
199
- children
184
+ children: /* @__PURE__ */ jsx(Slot, { children })
200
185
  }
201
186
  );
202
187
  });
@@ -218,10 +203,9 @@ export {
218
203
  BlocksProviderContext,
219
204
  Box,
220
205
  Divider,
221
- Slot,
222
206
  classnames,
223
207
  composeRefs,
224
- createSlottable,
208
+ createAsChildTemplate,
225
209
  useComponentStyleDefaultProps,
226
210
  useComponentStyles,
227
211
  useTheme
@@ -1,15 +1,15 @@
1
1
  "use strict";
2
2
  const jsxRuntime = require("react/jsx-runtime");
3
3
  const react = require("react");
4
- const styles_lib_utils_atomProps_cjs = require("../../../lib/utils/atom-props.cjs");
5
4
  const styles_components_display_Divider_Divider_cjs = require("../../display/Divider/Divider.cjs");
5
+ const styles_lib_utils_atomProps_cjs = require("../../../lib/utils/atom-props.cjs");
6
6
  const styles_components_form_Button_Button_css_cjs = require("./Button.css.cjs");
7
7
  const styles_lib_css_atoms_sprinkles_css_cjs = require("../../../lib/css/atoms/sprinkles.css.cjs");
8
8
  const Spinner = ({ className, size, color, ...restProps }) => {
9
9
  const spinnerClassName = styles_components_display_Divider_Divider_cjs.useComponentStyles("spinner", { base: true, variants: { size, color } });
10
10
  return /* @__PURE__ */ jsxRuntime.jsx(styles_components_display_Divider_Divider_cjs.Box, { color, className: styles_components_display_Divider_Divider_cjs.classnames(spinnerClassName, className), ...restProps });
11
11
  };
12
- const Slottable = styles_components_display_Divider_Divider_cjs.createSlottable("button");
12
+ const { Template, Slot } = styles_components_display_Divider_Divider_cjs.createAsChildTemplate("button");
13
13
  const Button = react.forwardRef(function Button2({
14
14
  children,
15
15
  className,
@@ -35,7 +35,7 @@ const Button = react.forwardRef(function Button2({
35
35
  });
36
36
  const [atomsProps, otherProps] = styles_lib_utils_atomProps_cjs.getAtomsAndProps(restProps);
37
37
  return /* @__PURE__ */ jsxRuntime.jsxs(
38
- Slottable,
38
+ Template,
39
39
  {
40
40
  ref,
41
41
  asChild,
@@ -45,7 +45,7 @@ const Button = react.forwardRef(function Button2({
45
45
  children: [
46
46
  startSlot && /* @__PURE__ */ jsxRuntime.jsx("div", { children: startSlot }),
47
47
  loading && /* @__PURE__ */ jsxRuntime.jsx(Spinner, { size }),
48
- /* @__PURE__ */ jsxRuntime.jsx(styles_components_display_Divider_Divider_cjs.Slot, { children }),
48
+ /* @__PURE__ */ jsxRuntime.jsx(Slot, { children }),
49
49
  endSlot && /* @__PURE__ */ jsxRuntime.jsx("div", { children: endSlot })
50
50
  ]
51
51
  }
@@ -1,14 +1,14 @@
1
1
  import { jsx, jsxs } from "react/jsx-runtime";
2
2
  import { forwardRef } from "react";
3
+ import { useComponentStyles, Box, classnames, createAsChildTemplate } from "../../display/Divider/Divider.mjs";
3
4
  import { getAtomsAndProps } from "../../../lib/utils/atom-props.mjs";
4
- import { useComponentStyles, Box, classnames, createSlottable, Slot } from "../../display/Divider/Divider.mjs";
5
5
  import { buttonReset } from "./Button.css.mjs";
6
6
  import { atoms } from "../../../lib/css/atoms/sprinkles.css.mjs";
7
7
  const Spinner = ({ className, size, color, ...restProps }) => {
8
8
  const spinnerClassName = useComponentStyles("spinner", { base: true, variants: { size, color } });
9
9
  return /* @__PURE__ */ jsx(Box, { color, className: classnames(spinnerClassName, className), ...restProps });
10
10
  };
11
- const Slottable = createSlottable("button");
11
+ const { Template, Slot } = createAsChildTemplate("button");
12
12
  const Button = forwardRef(function Button2({
13
13
  children,
14
14
  className,
@@ -34,7 +34,7 @@ const Button = forwardRef(function Button2({
34
34
  });
35
35
  const [atomsProps, otherProps] = getAtomsAndProps(restProps);
36
36
  return /* @__PURE__ */ jsxs(
37
- Slottable,
37
+ Template,
38
38
  {
39
39
  ref,
40
40
  asChild,
@@ -3,15 +3,6 @@ const jsxRuntime = require("react/jsx-runtime");
3
3
  const react = require("react");
4
4
  const styles_components_display_Divider_Divider_cjs = require("../../display/Divider/Divider.cjs");
5
5
  const styles_components_overlay_Dialog_dialog_css_cjs = require("./dialog.css.cjs");
6
- const reactDom = require("react-dom");
7
- const styles_components_other_BlocksProvider_BlocksProvider_cjs = require("../../other/BlocksProvider/BlocksProvider.cjs");
8
- const Portal = ({ children, container }) => {
9
- const context = styles_components_display_Divider_Divider_cjs.useTheme();
10
- return reactDom.createPortal(
11
- /* @__PURE__ */ jsxRuntime.jsx(styles_components_other_BlocksProvider_BlocksProvider_cjs.BlocksProvider, { theme: context, children }),
12
- container || document.body
13
- );
14
- };
15
6
  const useClickOutside = (ref, onClickOutside, { enabled = true } = {}) => {
16
7
  react.useEffect(() => {
17
8
  if (!enabled) {
@@ -22,8 +13,11 @@ const useClickOutside = (ref, onClickOutside, { enabled = true } = {}) => {
22
13
  onClickOutside();
23
14
  }
24
15
  };
25
- document.addEventListener("click", listener);
16
+ const rafId = requestAnimationFrame(() => {
17
+ document.addEventListener("click", listener);
18
+ });
26
19
  return () => {
20
+ cancelAnimationFrame(rafId);
27
21
  document.removeEventListener("click", listener);
28
22
  };
29
23
  }, [ref, onClickOutside, enabled]);
@@ -45,27 +39,6 @@ const useKeyboard = (key, callback, { enabled = true, type = "keydown" } = {}) =
45
39
  };
46
40
  }, [callback, enabled, key, type]);
47
41
  };
48
- const useLayer = () => {
49
- const layerRef = react.useRef();
50
- react.useEffect(
51
- () => () => {
52
- if (layerRef.current) {
53
- layerRef.current.remove();
54
- layerRef.current = void 0;
55
- }
56
- },
57
- []
58
- );
59
- return () => {
60
- if (!layerRef.current) {
61
- const div = document.createElement("div");
62
- div.dataset.layer = "blocks";
63
- layerRef.current = div;
64
- document.body.append(layerRef.current);
65
- }
66
- return layerRef.current;
67
- };
68
- };
69
42
  const usePreventBodyScroll = (enabled = true) => {
70
43
  useIsomorphicLayoutEffect(() => {
71
44
  if (!enabled) {
@@ -106,24 +79,15 @@ const useRestoreFocus = (active) => {
106
79
  }
107
80
  }, [active]);
108
81
  };
109
- const useVisibilityState = (open) => {
110
- const [visible, setVisible] = react.useState(open);
111
- const hide = react.useCallback(() => {
112
- setVisible(false);
113
- }, []);
114
- react.useEffect(() => {
115
- if (open) {
116
- setVisible(true);
117
- }
118
- }, [open]);
119
- return [visible, hide];
120
- };
121
82
  function hasAnimationDuration(element) {
122
83
  if (!element) {
123
84
  return false;
124
85
  }
125
86
  const style = window.getComputedStyle(element);
126
- return style.transitionDuration !== "0s" || style.animationDuration !== "0s";
87
+ return hasDuration(style.transitionDuration) || hasDuration(style.animationDuration);
88
+ }
89
+ function hasDuration(duration) {
90
+ return duration.split(",").map((part) => Number.parseFloat(part.trim())).some((part) => part > 0);
127
91
  }
128
92
  const DialogContext = react.createContext(void 0);
129
93
  const useNestedDialog = (open) => {
@@ -149,12 +113,11 @@ const Dialog = ({
149
113
  }) => {
150
114
  const dialogClassName = styles_components_display_Divider_Divider_cjs.useComponentStyles("dialog", { dialog: true, variants: { size } });
151
115
  const dialogRef = react.useRef(null);
152
- const layer = useLayer();
153
- const [visible, hide] = useVisibilityState(open);
154
116
  const [enabled, setEnabled] = react.useState(true);
117
+ const [visible, setVisible] = react.useState(open);
155
118
  useRestoreFocus(open);
156
- const isNested = useNestedDialog(visible);
157
- usePreventBodyScroll(visible && !isNested);
119
+ const isNested = useNestedDialog(open);
120
+ usePreventBodyScroll(open && !isNested);
158
121
  const onEscape = react.useCallback(
159
122
  (event) => {
160
123
  event.preventDefault();
@@ -166,47 +129,32 @@ const Dialog = ({
166
129
  useClickOutside(dialogRef, onRequestClose, { enabled: open && enabled });
167
130
  useIsomorphicLayoutEffect(() => {
168
131
  var _a, _b;
169
- if (!visible) {
170
- (_a = dialogRef.current) == null ? void 0 : _a.close();
171
- }
172
- if (!open) {
173
- (_b = dialogRef.current) == null ? void 0 : _b.removeAttribute("data-open");
174
- return;
132
+ if (open && visible) {
133
+ (_a = dialogRef.current) == null ? void 0 : _a.showModal();
134
+ } else if (open) {
135
+ setVisible(true);
136
+ } else {
137
+ if (!hasAnimationDuration(dialogRef.current)) {
138
+ setVisible(false);
139
+ }
140
+ (_b = dialogRef.current) == null ? void 0 : _b.close();
175
141
  }
176
- let timer = requestAnimationFrame(() => {
177
- timer = requestAnimationFrame(() => {
178
- var _a2, _b2;
179
- (_a2 = dialogRef.current) == null ? void 0 : _a2.showModal();
180
- (_b2 = dialogRef.current) == null ? void 0 : _b2.setAttribute("data-open", "");
181
- });
182
- });
183
- return () => {
184
- cancelAnimationFrame(timer);
185
- };
186
142
  }, [open, visible]);
187
143
  const onAnimationEnd = react.useCallback(() => {
188
144
  if (!open) {
189
- hide();
190
- }
191
- }, [hide, open]);
192
- react.useEffect(() => {
193
- if (open) {
194
- return;
195
- }
196
- if (!hasAnimationDuration(dialogRef.current)) {
197
- hide();
145
+ setVisible(false);
198
146
  }
199
- }, [hide, open]);
147
+ }, [setVisible, open]);
200
148
  if (!visible) {
201
149
  return null;
202
150
  }
203
- const dataOpen = typeof window === "undefined" && open ? "" : void 0;
204
- return /* @__PURE__ */ jsxRuntime.jsx(Portal, { container: layer(), children: /* @__PURE__ */ jsxRuntime.jsx(DialogContext.Provider, { value: { setEnabled }, children: /* @__PURE__ */ jsxRuntime.jsx(
151
+ const dataOpen = typeof window === "undefined" && open ? true : void 0;
152
+ return /* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children: /* @__PURE__ */ jsxRuntime.jsx(DialogContext.Provider, { value: { setEnabled }, children: /* @__PURE__ */ jsxRuntime.jsx(
205
153
  "dialog",
206
154
  {
207
155
  ref: dialogRef,
208
156
  "aria-modal": "true",
209
- "data-open": dataOpen,
157
+ open: dataOpen,
210
158
  className: styles_components_display_Divider_Divider_cjs.classnames(styles_components_overlay_Dialog_dialog_css_cjs.dialog, dialogClassName, className),
211
159
  onAnimationEnd,
212
160
  onTransitionEnd: onAnimationEnd,
@@ -216,11 +164,8 @@ const Dialog = ({
216
164
  ) }) });
217
165
  };
218
166
  exports.Dialog = Dialog;
219
- exports.Portal = Portal;
220
167
  exports.hasAnimationDuration = hasAnimationDuration;
221
168
  exports.useClickOutside = useClickOutside;
222
169
  exports.useIsomorphicLayoutEffect = useIsomorphicLayoutEffect;
223
170
  exports.useKeyboard = useKeyboard;
224
- exports.useLayer = useLayer;
225
171
  exports.usePreventBodyScroll = usePreventBodyScroll;
226
- exports.useVisibilityState = useVisibilityState;