@zuzjs/ui 0.9.0 → 0.9.1

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.
@@ -6,20 +6,7 @@ const Box = forwardRef((props, ref) => {
6
6
  const innerRef = useRef(null);
7
7
  const targetRef = useMemo(() => ref && typeof ref !== "function" && ref.current ? ref : innerRef, [ref]);
8
8
  const { style: _style, className, rest } = useBase(pops, targetRef);
9
- // const handleInternalClick = (e: React.MouseEvent<HTMLDivElement>) => {
10
- // // if ( withEditor && isBrowser ) {
11
- // // // window.dispatchEvent(new CustomEvent(`ZUZ_COMP_SELECTED`, {
12
- // // // detail: {
13
- // // // compName: 'Box',
14
- // // // target: e.target,
15
- // // // props
16
- // // // }
17
- // // // }))
18
- // // }
19
- // }
20
- return _jsx("div", { ref: ref || innerRef,
21
- // onClick={handleInternalClick}
22
- className: `${className} ${withEditor ? `--with-zuz-editor` : ``}`.trim(), style: {
9
+ return _jsx("div", { ref: ref || innerRef, className: `${className} ${withEditor ? `--with-zuz-editor` : ``}`.trim(), style: {
23
10
  ..._style,
24
11
  ...(style || {})
25
12
  }, ...rest });
@@ -23,7 +23,7 @@ const Password = forwardRef((props, ref) => {
23
23
  onChange && onChange(ev);
24
24
  }, 100);
25
25
  useEffect(() => { }, []);
26
- return _jsxs(Box, { as: `w:100% flex cols`, children: [_jsxs(Box, { style: style, className: `--password flex aic rel`, children: [_jsx(Input, { ref: ref, type: visible ? 'text' : 'password', className: className, onChange: debounce, ...rest }), _jsx(Button, { tabIndex: -1, onClick: () => setVisible(prev => !prev), className: `--toggle flex aic jcc abs`, children: visible ? SVGIcons.eye : SVGIcons.eyeSlash })] }), strenthMeter && _jsxs(Box, { as: `flex aic --password-meter --pb-score-${strenth.score}`, children: [_jsxs(Box, { as: `--password-bars flex aic`, children: [_jsx(Box, { as: `--pbar ${strenth.score >= 1 ? `--pb-on` : ``}`.trim() }), _jsx(Box, { as: `--pbar ${strenth.score >= 2 ? `--pb-on` : ``}`.trim() }), _jsx(Box, { as: `--pbar ${strenth.score >= 3 ? `--pb-on` : ``}`.trim() }), _jsx(Box, { as: `--pbar ${strenth.score >= 4 ? `--pb-on` : ``}`.trim() }), _jsx(Box, { as: `--pbar ${strenth.score >= 5 ? `--pb-on` : ``}`.trim() })] }), _jsx(Text, { as: `--password-meter-label`, "aria-hidden": true, children: strenth.result })] })] });
26
+ return _jsxs(Box, { as: `w:100% flex cols`, children: [_jsxs(Box, { style: style, className: `--password flex aic rel`, children: [_jsx(Input, { ref: ref, type: visible ? 'text' : 'password', className: className, onChange: debounce, ...rest }), _jsx(Button, { tabIndex: -1, onClick: () => setVisible(prev => !prev), className: `--toggle flex aic jcc abs`, children: visible ? SVGIcons.eye : SVGIcons.eyeSlash })] }), strenthMeter && _jsxs(Box, { as: `flex aic --password-meter --pb-score-${strenth.score}`, style: style, children: [_jsxs(Box, { as: `--password-bars flex aic`, children: [_jsx(Box, { as: `--pbar ${strenth.score >= 1 ? `--pb-on` : ``}`.trim() }), _jsx(Box, { as: `--pbar ${strenth.score >= 2 ? `--pb-on` : ``}`.trim() }), _jsx(Box, { as: `--pbar ${strenth.score >= 3 ? `--pb-on` : ``}`.trim() }), _jsx(Box, { as: `--pbar ${strenth.score >= 4 ? `--pb-on` : ``}`.trim() }), _jsx(Box, { as: `--pbar ${strenth.score >= 5 ? `--pb-on` : ``}`.trim() })] }), _jsx(Text, { as: `--password-meter-label`, "aria-hidden": true, children: strenth.result })] })] });
27
27
  });
28
28
  Password.displayName = `Zuz.Password`;
29
29
  export default Password;
@@ -1,6 +1,6 @@
1
1
  import { Variant } from "../../types/enums";
2
2
  import { BoxProps } from "../Box";
3
- import { Segment } from "./types";
3
+ import { Segment, SegmentController } from "./types";
4
4
  /**
5
5
  * `SelectTabs` component is a segmented control that allows switching between segments.
6
6
  *
@@ -25,5 +25,5 @@ declare const Segmented: import("react").ForwardRefExoticComponent<BoxProps & {
25
25
  selected?: number;
26
26
  onSwitch?: (segment: Segment) => void;
27
27
  items: Segment[];
28
- } & import("react").RefAttributes<HTMLDivElement>>;
28
+ } & import("react").RefAttributes<SegmentController>>;
29
29
  export default Segmented;
@@ -1,6 +1,6 @@
1
1
  'use client';
2
2
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
- import { forwardRef, useEffect, useRef, useState } from "react";
3
+ import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from "react";
4
4
  import { useBase } from "../../hooks";
5
5
  import { Variant } from "../../types/enums";
6
6
  import Box from "../Box";
@@ -26,6 +26,8 @@ import SegmentItem from "./item";
26
26
  const Segmented = forwardRef((props, ref) => {
27
27
  const { animate, fx, items, selected, size, variant, onSwitch, ...pops } = props;
28
28
  const [_selected, setSelected] = useState(selected || 0);
29
+ const hasMounted = useRef(false);
30
+ const prevSelectedRef = useRef(_selected);
29
31
  const { className, style, rest } = useBase(pops);
30
32
  const _tab = useRef(null);
31
33
  const _segmented = useRef(null);
@@ -40,8 +42,9 @@ const Segmented = forwardRef((props, ref) => {
40
42
  // console.log(selected, _selected, index, mounted)
41
43
  if (force || (_selected != index && _selected != -2)) {
42
44
  setSelected(index);
43
- if (onSwitch)
44
- onSwitch(meta);
45
+ prevSelectedRef.current = _selected;
46
+ if (hasMounted.current)
47
+ onSwitch?.(meta);
45
48
  }
46
49
  if (_tab.current) {
47
50
  const _sp = _segmented.current?.getBoundingClientRect();
@@ -49,11 +52,22 @@ const Segmented = forwardRef((props, ref) => {
49
52
  _tab.current.style.setProperty(`--x`, `${_sp ? x - _sp.left : x}px`);
50
53
  }
51
54
  };
52
- useEffect(() => {
53
- if (selected !== undefined && selected != _selected) {
54
- setSelected(selected);
55
+ useImperativeHandle(ref, () => ({
56
+ setSelected: (index) => {
57
+ if (typeof index === `number` && index != _selected) {
58
+ setSelected(index);
59
+ }
55
60
  }
56
- }, [selected, _selected]);
61
+ }), [_selected]);
62
+ // useEffect(() => {
63
+ // if (typeof selected === `number` && selected != _selected) {
64
+ // prevSelectedRef.current = _selected;
65
+ // setSelected(selected);
66
+ // }
67
+ // }, [selected, _selected]);
68
+ useEffect(() => {
69
+ hasMounted.current = true;
70
+ }, []);
57
71
  return _jsxs(Box, { ref: _segmented, "data-selected": _selected, className: `${className} --segmented --${variant || Variant.Small} flex aic rel`, style: style, ...rest, children: [_jsx(Box, { ref: _tab, className: `--segment-tab abs` }), items.map((item, i) => _jsx(SegmentItem, { onSelect: handleSelect, selected: _selected == i, meta: {
58
72
  ...item,
59
73
  index: i
@@ -36,3 +36,6 @@ export type SegmentItemProps = {
36
36
  selected: boolean;
37
37
  onSelect: (index: number, width: number, x: number, segment: Segment, force: boolean) => void;
38
38
  };
39
+ export interface SegmentController {
40
+ setSelected: (index: number) => void;
41
+ }
@@ -6,20 +6,7 @@ const Box = forwardRef((props, ref) => {
6
6
  const innerRef = useRef(null);
7
7
  const targetRef = useMemo(() => ref && typeof ref !== "function" && ref.current ? ref : innerRef, [ref]);
8
8
  const { style: _style, className, rest } = useBase(pops, targetRef);
9
- // const handleInternalClick = (e: React.MouseEvent<HTMLDivElement>) => {
10
- // // if ( withEditor && isBrowser ) {
11
- // // // window.dispatchEvent(new CustomEvent(`ZUZ_COMP_SELECTED`, {
12
- // // // detail: {
13
- // // // compName: 'Box',
14
- // // // target: e.target,
15
- // // // props
16
- // // // }
17
- // // // }))
18
- // // }
19
- // }
20
- return _jsx("div", { ref: ref || innerRef,
21
- // onClick={handleInternalClick}
22
- className: `${className} ${withEditor ? `--with-zuz-editor` : ``}`.trim(), style: {
9
+ return _jsx("div", { ref: ref || innerRef, className: `${className} ${withEditor ? `--with-zuz-editor` : ``}`.trim(), style: {
23
10
  ..._style,
24
11
  ...(style || {})
25
12
  }, ...rest });
@@ -23,7 +23,7 @@ const Password = forwardRef((props, ref) => {
23
23
  onChange && onChange(ev);
24
24
  }, 100);
25
25
  useEffect(() => { }, []);
26
- return _jsxs(Box, { as: `w:100% flex cols`, children: [_jsxs(Box, { style: style, className: `--password flex aic rel`, children: [_jsx(Input, { ref: ref, type: visible ? 'text' : 'password', className: className, onChange: debounce, ...rest }), _jsx(Button, { tabIndex: -1, onClick: () => setVisible(prev => !prev), className: `--toggle flex aic jcc abs`, children: visible ? SVGIcons.eye : SVGIcons.eyeSlash })] }), strenthMeter && _jsxs(Box, { as: `flex aic --password-meter --pb-score-${strenth.score}`, children: [_jsxs(Box, { as: `--password-bars flex aic`, children: [_jsx(Box, { as: `--pbar ${strenth.score >= 1 ? `--pb-on` : ``}`.trim() }), _jsx(Box, { as: `--pbar ${strenth.score >= 2 ? `--pb-on` : ``}`.trim() }), _jsx(Box, { as: `--pbar ${strenth.score >= 3 ? `--pb-on` : ``}`.trim() }), _jsx(Box, { as: `--pbar ${strenth.score >= 4 ? `--pb-on` : ``}`.trim() }), _jsx(Box, { as: `--pbar ${strenth.score >= 5 ? `--pb-on` : ``}`.trim() })] }), _jsx(Text, { as: `--password-meter-label`, "aria-hidden": true, children: strenth.result })] })] });
26
+ return _jsxs(Box, { as: `w:100% flex cols`, children: [_jsxs(Box, { style: style, className: `--password flex aic rel`, children: [_jsx(Input, { ref: ref, type: visible ? 'text' : 'password', className: className, onChange: debounce, ...rest }), _jsx(Button, { tabIndex: -1, onClick: () => setVisible(prev => !prev), className: `--toggle flex aic jcc abs`, children: visible ? SVGIcons.eye : SVGIcons.eyeSlash })] }), strenthMeter && _jsxs(Box, { as: `flex aic --password-meter --pb-score-${strenth.score}`, style: style, children: [_jsxs(Box, { as: `--password-bars flex aic`, children: [_jsx(Box, { as: `--pbar ${strenth.score >= 1 ? `--pb-on` : ``}`.trim() }), _jsx(Box, { as: `--pbar ${strenth.score >= 2 ? `--pb-on` : ``}`.trim() }), _jsx(Box, { as: `--pbar ${strenth.score >= 3 ? `--pb-on` : ``}`.trim() }), _jsx(Box, { as: `--pbar ${strenth.score >= 4 ? `--pb-on` : ``}`.trim() }), _jsx(Box, { as: `--pbar ${strenth.score >= 5 ? `--pb-on` : ``}`.trim() })] }), _jsx(Text, { as: `--password-meter-label`, "aria-hidden": true, children: strenth.result })] })] });
27
27
  });
28
28
  Password.displayName = `Zuz.Password`;
29
29
  export default Password;
@@ -1,6 +1,6 @@
1
1
  import { Variant } from "../../types/enums";
2
2
  import { BoxProps } from "../Box";
3
- import { Segment } from "./types";
3
+ import { Segment, SegmentController } from "./types";
4
4
  /**
5
5
  * `SelectTabs` component is a segmented control that allows switching between segments.
6
6
  *
@@ -25,5 +25,5 @@ declare const Segmented: import("react").ForwardRefExoticComponent<BoxProps & {
25
25
  selected?: number;
26
26
  onSwitch?: (segment: Segment) => void;
27
27
  items: Segment[];
28
- } & import("react").RefAttributes<HTMLDivElement>>;
28
+ } & import("react").RefAttributes<SegmentController>>;
29
29
  export default Segmented;
@@ -1,6 +1,6 @@
1
1
  'use client';
2
2
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
- import { forwardRef, useEffect, useRef, useState } from "react";
3
+ import { forwardRef, useEffect, useImperativeHandle, useRef, useState } from "react";
4
4
  import { useBase } from "../../hooks";
5
5
  import { Variant } from "../../types/enums";
6
6
  import Box from "../Box";
@@ -26,6 +26,8 @@ import SegmentItem from "./item";
26
26
  const Segmented = forwardRef((props, ref) => {
27
27
  const { animate, fx, items, selected, size, variant, onSwitch, ...pops } = props;
28
28
  const [_selected, setSelected] = useState(selected || 0);
29
+ const hasMounted = useRef(false);
30
+ const prevSelectedRef = useRef(_selected);
29
31
  const { className, style, rest } = useBase(pops);
30
32
  const _tab = useRef(null);
31
33
  const _segmented = useRef(null);
@@ -40,8 +42,9 @@ const Segmented = forwardRef((props, ref) => {
40
42
  // console.log(selected, _selected, index, mounted)
41
43
  if (force || (_selected != index && _selected != -2)) {
42
44
  setSelected(index);
43
- if (onSwitch)
44
- onSwitch(meta);
45
+ prevSelectedRef.current = _selected;
46
+ if (hasMounted.current)
47
+ onSwitch?.(meta);
45
48
  }
46
49
  if (_tab.current) {
47
50
  const _sp = _segmented.current?.getBoundingClientRect();
@@ -49,11 +52,22 @@ const Segmented = forwardRef((props, ref) => {
49
52
  _tab.current.style.setProperty(`--x`, `${_sp ? x - _sp.left : x}px`);
50
53
  }
51
54
  };
52
- useEffect(() => {
53
- if (selected !== undefined && selected != _selected) {
54
- setSelected(selected);
55
+ useImperativeHandle(ref, () => ({
56
+ setSelected: (index) => {
57
+ if (typeof index === `number` && index != _selected) {
58
+ setSelected(index);
59
+ }
55
60
  }
56
- }, [selected, _selected]);
61
+ }), [_selected]);
62
+ // useEffect(() => {
63
+ // if (typeof selected === `number` && selected != _selected) {
64
+ // prevSelectedRef.current = _selected;
65
+ // setSelected(selected);
66
+ // }
67
+ // }, [selected, _selected]);
68
+ useEffect(() => {
69
+ hasMounted.current = true;
70
+ }, []);
57
71
  return _jsxs(Box, { ref: _segmented, "data-selected": _selected, className: `${className} --segmented --${variant || Variant.Small} flex aic rel`, style: style, ...rest, children: [_jsx(Box, { ref: _tab, className: `--segment-tab abs` }), items.map((item, i) => _jsx(SegmentItem, { onSelect: handleSelect, selected: _selected == i, meta: {
58
72
  ...item,
59
73
  index: i
@@ -36,3 +36,6 @@ export type SegmentItemProps = {
36
36
  selected: boolean;
37
37
  onSelect: (index: number, width: number, x: number, segment: Segment, force: boolean) => void;
38
38
  };
39
+ export interface SegmentController {
40
+ setSelected: (index: number) => void;
41
+ }