@lindle/linoardo 1.0.13 → 1.0.15

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.ts CHANGED
@@ -466,6 +466,15 @@ interface ExpansionPanelMultipleProps extends ExpansionPanelSharedProps {
466
466
  onChange?: (value: ExpansionPanelValue[]) => void;
467
467
  }
468
468
  type ExpansionPanelProps = ExpansionPanelSingleProps | ExpansionPanelMultipleProps;
469
+ interface ExpansionPanelContextValue {
470
+ expandedValues: ExpansionPanelValue[];
471
+ toggle: (value: ExpansionPanelValue, disabled?: boolean) => void;
472
+ density: ExpansionPanelDensity;
473
+ color: Palette;
474
+ divider: boolean;
475
+ rounded: ExpansionPanelRounded;
476
+ variant: ExpansionPanelVariant;
477
+ }
469
478
  interface ExpansionPanelItemProps extends Omit<DivAttributes, 'title'> {
470
479
  value?: ExpansionPanelValue;
471
480
  title?: react.ReactNode;
@@ -480,6 +489,8 @@ interface ExpansionPanelItemProps extends Omit<DivAttributes, 'title'> {
480
489
  headerClassName?: string;
481
490
  contentClassName?: string;
482
491
  color?: Palette;
492
+ /** @internal */
493
+ __expansionPanelContext?: ExpansionPanelContextValue | null;
483
494
  }
484
495
 
485
496
  declare const ExpansionPanelItem: react.ForwardRefExoticComponent<ExpansionPanelItemProps & react.RefAttributes<HTMLDivElement>>;
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as React3 from 'react';
2
- import { forwardRef, Component } from 'react';
2
+ import { forwardRef, Component, useState, useRef, useEffect, useCallback, useMemo, isValidElement, cloneElement } from 'react';
3
3
  import { twMerge } from 'tailwind-merge';
4
4
  import { jsxs, jsx, Fragment } from 'react/jsx-runtime';
5
5
 
@@ -466,20 +466,20 @@ var ListItem = React3.forwardRef((props, ref) => {
466
466
  const sharp = sharpOverride ?? false;
467
467
  const accent = accentClasses[color] ?? accentClasses.primary;
468
468
  const shapeClass = divided || sharp ? "rounded-none" : "rounded-lg";
469
- const Component3 = component ?? (href ? "a" : "div");
470
- const interactive = typeof rest.onClick === "function" || Component3 === "a" || Component3 === "button";
469
+ const Component2 = component ?? (href ? "a" : "div");
470
+ const interactive = typeof rest.onClick === "function" || Component2 === "a" || Component2 === "button";
471
471
  const resolvedRole = role ?? "listitem";
472
- const resolvedTabIndex = disabled ? -1 : tabIndex ?? (interactive && Component3 === "div" ? 0 : void 0);
473
- const resolvedRel = Component3 === "a" ? rel : void 0;
474
- const resolvedTarget = Component3 === "a" ? target : void 0;
475
- const resolvedHref = Component3 === "a" ? href : void 0;
476
- const resolvedType = Component3 === "button" ? type ?? "button" : void 0;
472
+ const resolvedTabIndex = disabled ? -1 : tabIndex ?? (interactive && Component2 === "div" ? 0 : void 0);
473
+ const resolvedRel = Component2 === "a" ? rel : void 0;
474
+ const resolvedTarget = Component2 === "a" ? target : void 0;
475
+ const resolvedHref = Component2 === "a" ? href : void 0;
476
+ const resolvedType = Component2 === "button" ? type ?? "button" : void 0;
477
477
  const disabledClass = disabled ? "pointer-events-none opacity-60" : "cursor-pointer";
478
478
  const navPaddingClass = nav ? "pl-5" : void 0;
479
479
  const insetClass = inset ? "pl-12" : void 0;
480
480
  const activeClasses = active ? accent.bg : void 0;
481
481
  return /* @__PURE__ */ jsxs(
482
- Component3,
482
+ Component2,
483
483
  {
484
484
  ...rest,
485
485
  ref,
@@ -702,7 +702,55 @@ var Menu = React3.forwardRef((props, ref) => {
702
702
  });
703
703
  Menu.displayName = "Menu";
704
704
  var Menu_default = Menu;
705
- var ExpansionPanelContext = React3.createContext(null);
705
+ var EXPANSION_PANEL_CONTEXT_PROP = "__expansionPanelContext";
706
+ var EXPANSION_PANEL_ITEM_MARKER = "__isExpansionPanelItem";
707
+ var hasMarker = (type) => {
708
+ if (!type || typeof type !== "function" && typeof type !== "object") {
709
+ return false;
710
+ }
711
+ if (type[EXPANSION_PANEL_ITEM_MARKER]) {
712
+ return true;
713
+ }
714
+ const innerType = type.type;
715
+ if (innerType && innerType !== type) {
716
+ return hasMarker(innerType);
717
+ }
718
+ return false;
719
+ };
720
+ var traverseNode = (node, value) => {
721
+ if (Array.isArray(node)) {
722
+ let changed = false;
723
+ const nextArray = node.map((child) => {
724
+ const result = traverseNode(child, value);
725
+ if (result.changed) {
726
+ changed = true;
727
+ }
728
+ return result.node;
729
+ });
730
+ return { node: changed ? nextArray : node, changed };
731
+ }
732
+ if (node === null || node === void 0 || typeof node === "boolean" || typeof node === "string" || typeof node === "number") {
733
+ return { node, changed: false };
734
+ }
735
+ if (!isValidElement(node)) {
736
+ return { node, changed: false };
737
+ }
738
+ const { node: mappedChildren, changed: childrenChanged } = traverseNode(node.props.children, value);
739
+ const shouldInject = hasMarker(node.type);
740
+ if (!shouldInject && !childrenChanged) {
741
+ return { node, changed: false };
742
+ }
743
+ const injectedProps = shouldInject ? { [EXPANSION_PANEL_CONTEXT_PROP]: value } : void 0;
744
+ const cloned = mappedChildren === void 0 ? cloneElement(node, injectedProps) : cloneElement(node, injectedProps, mappedChildren);
745
+ return { node: cloned, changed: true };
746
+ };
747
+ var injectExpansionPanelContext = (children, value) => traverseNode(children, value).node;
748
+ var markExpansionPanelItem = (component) => {
749
+ if (typeof component !== "function" && (typeof component !== "object" || component === null)) {
750
+ return;
751
+ }
752
+ component[EXPANSION_PANEL_ITEM_MARKER] = true;
753
+ };
706
754
  var densityClasses2 = {
707
755
  comfortable: "py-5",
708
756
  default: "py-4",
@@ -736,12 +784,13 @@ var generateId = (prefix) => `${prefix}-${++uniqueIdCounter}`;
736
784
  var ExpansionPanelItemInner = class extends Component {
737
785
  constructor(props) {
738
786
  super(props);
787
+ this.getContext = () => this.props.__expansionPanelContext ?? null;
739
788
  this.handleToggle = () => {
740
789
  const { disabled = false } = this.props;
741
790
  if (disabled) {
742
791
  return;
743
792
  }
744
- const context = this.context;
793
+ const context = this.getContext();
745
794
  const panelValue = this.props.value ?? this.generatedValue;
746
795
  if (context) {
747
796
  context.toggle(panelValue, disabled);
@@ -774,7 +823,7 @@ var ExpansionPanelItemInner = class extends Component {
774
823
  forwardedRef,
775
824
  ...rest
776
825
  } = this.props;
777
- const context = this.context;
826
+ const context = this.getContext();
778
827
  const panelValue = value ?? this.generatedValue;
779
828
  const density = context?.density ?? "default";
780
829
  const color = colorOverride ?? context?.color ?? "primary";
@@ -871,9 +920,9 @@ var ExpansionPanelItemInner = class extends Component {
871
920
  );
872
921
  }
873
922
  };
874
- ExpansionPanelItemInner.contextType = ExpansionPanelContext;
875
923
  var ExpansionPanelItem = forwardRef((props, ref) => /* @__PURE__ */ jsx(ExpansionPanelItemInner, { ...props, forwardedRef: ref }));
876
924
  ExpansionPanelItem.displayName = "ExpansionPanelItem";
925
+ markExpansionPanelItem(ExpansionPanelItem);
877
926
  var ExpansionPanelItem_default = ExpansionPanelItem;
878
927
  var variantContainerClasses = {
879
928
  elevated: "bg-white border border-gray-200 shadow-lg shadow-gray-900/10",
@@ -900,22 +949,41 @@ var normalizeValues = (value, allowMultiple) => {
900
949
  return normalized.length ? [normalized[0]] : [];
901
950
  };
902
951
  var clampValues = (values, allowMultiple) => allowMultiple ? uniqueValues(values) : values.length ? [values[0]] : [];
903
- var ExpansionPanelInner = class extends Component {
904
- constructor(props) {
905
- super(props);
906
- this.isControlled = () => this.props.value !== void 0;
907
- this.getExpandedValues = (allowMultiple = this.props.multiple ?? false) => {
908
- if (this.isControlled()) {
909
- return normalizeValues(this.props.value, allowMultiple);
910
- }
911
- return this.state.internalValues;
912
- };
913
- this.handleValueChange = (next) => {
914
- const { onChange } = this.props;
915
- const allowMultiple = this.props.multiple ?? false;
916
- const isControlled = this.isControlled();
952
+ var ExpansionPanelInner = (props, forwardedRef) => {
953
+ const {
954
+ variant = "elevated",
955
+ rounded = "lg",
956
+ density = "default",
957
+ color = "primary",
958
+ divider = true,
959
+ multiple = false,
960
+ className,
961
+ children,
962
+ value,
963
+ defaultValue,
964
+ onChange,
965
+ ...rest
966
+ } = props;
967
+ const allowMultiple = multiple ?? false;
968
+ const isControlled = value !== void 0;
969
+ const [internalValues, setInternalValues] = useState(
970
+ () => normalizeValues(defaultValue, allowMultiple)
971
+ );
972
+ const prevAllowMultipleRef = useRef(allowMultiple);
973
+ const prevIsControlledRef = useRef(isControlled);
974
+ useEffect(() => {
975
+ const prevAllowMultiple = prevAllowMultipleRef.current;
976
+ const wasControlled = prevIsControlledRef.current;
977
+ if (!isControlled && (allowMultiple !== prevAllowMultiple || wasControlled !== isControlled)) {
978
+ setInternalValues((prev) => clampValues(prev, allowMultiple));
979
+ }
980
+ prevAllowMultipleRef.current = allowMultiple;
981
+ prevIsControlledRef.current = isControlled;
982
+ }, [allowMultiple, isControlled]);
983
+ const handleValueChange = useCallback(
984
+ (next) => {
917
985
  if (!isControlled) {
918
- this.setState({ internalValues: next });
986
+ setInternalValues(next);
919
987
  }
920
988
  if (onChange) {
921
989
  if (allowMultiple) {
@@ -924,74 +992,52 @@ var ExpansionPanelInner = class extends Component {
924
992
  onChange(next[0] ?? null);
925
993
  }
926
994
  }
927
- };
928
- this.handleToggle = (panelValue, disabled) => {
995
+ },
996
+ [allowMultiple, isControlled, onChange]
997
+ );
998
+ const handleToggle = useCallback(
999
+ (panelValue, disabled) => {
929
1000
  if (disabled) {
930
1001
  return;
931
1002
  }
932
- const allowMultiple = this.props.multiple ?? false;
933
- const expandedValues = this.getExpandedValues(allowMultiple);
934
- const isActive = expandedValues.includes(panelValue);
935
- const next = allowMultiple ? isActive ? expandedValues.filter((v) => v !== panelValue) : [...expandedValues, panelValue] : isActive ? [] : [panelValue];
936
- this.handleValueChange(next);
937
- };
938
- const allowMultiple = props.multiple ?? false;
939
- this.state = {
940
- internalValues: normalizeValues(props.defaultValue, allowMultiple)
941
- };
942
- }
943
- componentDidUpdate(prevProps) {
944
- const allowMultiple = this.props.multiple ?? false;
945
- const prevAllowMultiple = prevProps.multiple ?? false;
946
- const isControlled = this.isControlled();
947
- const wasControlled = prevProps.value !== void 0;
948
- if (!isControlled && (allowMultiple !== prevAllowMultiple || wasControlled !== isControlled)) {
949
- this.setState((prevState) => ({
950
- internalValues: clampValues(prevState.internalValues, allowMultiple)
951
- }));
952
- }
953
- }
954
- render() {
955
- const {
956
- variant = "elevated",
957
- rounded = "lg",
958
- density = "default",
959
- color = "primary",
960
- divider = true,
961
- multiple = false,
962
- className,
963
- children,
964
- forwardedRef,
965
- value: _value,
966
- defaultValue: _defaultValue,
967
- onChange: _onChange,
968
- ...rest
969
- } = this.props;
970
- const expandedValues = this.getExpandedValues(multiple);
971
- const providerValue = {
1003
+ const expandedValues2 = isControlled ? normalizeValues(value, allowMultiple) : internalValues;
1004
+ const isActive = expandedValues2.includes(panelValue);
1005
+ const next = allowMultiple ? isActive ? expandedValues2.filter((v) => v !== panelValue) : [...expandedValues2, panelValue] : isActive ? [] : [panelValue];
1006
+ handleValueChange(next);
1007
+ },
1008
+ [allowMultiple, handleValueChange, internalValues, isControlled, value]
1009
+ );
1010
+ const expandedValues = useMemo(
1011
+ () => isControlled ? normalizeValues(value, allowMultiple) : internalValues,
1012
+ [allowMultiple, internalValues, isControlled, value]
1013
+ );
1014
+ const providerValue = useMemo(
1015
+ () => ({
972
1016
  expandedValues,
973
- toggle: this.handleToggle,
1017
+ toggle: handleToggle,
974
1018
  density,
975
1019
  color,
976
1020
  divider,
977
1021
  rounded,
978
1022
  variant
979
- };
980
- const variantClass = divider ? variantContainerClasses[variant] : "bg-transparent border border-transparent shadow-none";
981
- const shapeClass = roundedClasses2[rounded] ?? roundedClasses2.lg;
982
- const layoutClass = divider ? "divide-y divide-gray-100 overflow-hidden" : "gap-4";
983
- return /* @__PURE__ */ jsx(ExpansionPanelContext.Provider, { value: providerValue, children: /* @__PURE__ */ jsx(
984
- "div",
985
- {
986
- ...rest,
987
- ref: forwardedRef,
988
- className: twMerge("expansion-panel flex w-full flex-col text-gray-900", variantClass, shapeClass, layoutClass, className),
989
- children
990
- }
991
- ) });
992
- }
1023
+ }),
1024
+ [color, density, divider, expandedValues, handleToggle, rounded, variant]
1025
+ );
1026
+ const variantClass = divider ? variantContainerClasses[variant] : "bg-transparent border border-transparent shadow-none";
1027
+ const shapeClass = roundedClasses2[rounded] ?? roundedClasses2.lg;
1028
+ const layoutClass = divider ? "divide-y divide-gray-100 overflow-hidden" : "gap-4";
1029
+ const enhancedChildren = injectExpansionPanelContext(children, providerValue);
1030
+ return /* @__PURE__ */ jsx(
1031
+ "div",
1032
+ {
1033
+ ...rest,
1034
+ ref: forwardedRef,
1035
+ className: twMerge("expansion-panel flex w-full flex-col text-gray-900", variantClass, shapeClass, layoutClass, className),
1036
+ children: enhancedChildren
1037
+ }
1038
+ );
993
1039
  };
994
- var ExpansionPanel = forwardRef((props, ref) => /* @__PURE__ */ jsx(ExpansionPanelInner, { ...props, forwardedRef: ref }));
1040
+ var ExpansionPanel = forwardRef(ExpansionPanelInner);
995
1041
  ExpansionPanel.displayName = "ExpansionPanel";
996
1042
  var ExpansionPanel_default = ExpansionPanel;
997
1043
  var containerBaseClasses = "fixed inset-0 z-[70] flex items-center justify-center p-4 sm:p-8 data-[state=closed]:pointer-events-none";