react-better-html 1.1.80 → 1.1.81

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.mjs CHANGED
@@ -1471,6 +1471,10 @@ var appConfig = {
1471
1471
  // src/components/BetterHtmlProvider.tsx
1472
1472
  import { Fragment, jsx, jsxs } from "react/jsx-runtime";
1473
1473
  var GlobalStyle = createGlobalStyle`
1474
+ html {
1475
+ background-color: ${(props) => props.backgroundColor};
1476
+ }
1477
+
1474
1478
  body {
1475
1479
  font-family: ${(props) => props.fontFamily};
1476
1480
  color: ${(props) => props.color};
@@ -1501,7 +1505,7 @@ var useBetterHtmlContext = () => {
1501
1505
  throw new Error(
1502
1506
  "`useBetterHtmlContext()` must be used within a `<BetterHtmlProvider>`. Make sure to add one at the root of your component tree."
1503
1507
  );
1504
- const { setLoaders, plugins, tabsComponentState, ...rest } = context;
1508
+ const { setLoaders, plugins, componentsState, ...rest } = context;
1505
1509
  return rest;
1506
1510
  };
1507
1511
  var useBetterHtmlContextInternal = () => {
@@ -1622,11 +1626,13 @@ function BetterHtmlProvider({ value, plugins: pluginsToUse, children }) {
1622
1626
  ...value?.components
1623
1627
  },
1624
1628
  plugins,
1625
- tabsComponentState: {
1626
- tabGroups,
1627
- setTabGroups,
1628
- tabsWithDots,
1629
- setTabsWithDots
1629
+ componentsState: {
1630
+ tabs: {
1631
+ tabGroups,
1632
+ setTabGroups,
1633
+ tabsWithDots,
1634
+ setTabsWithDots
1635
+ }
1630
1636
  }
1631
1637
  }),
1632
1638
  [value, colorTheme, loaders, plugins, tabGroups, tabsWithDots]
@@ -6530,7 +6536,8 @@ var TabsComponent = function Tabs({
6530
6536
  const reactRouterDomPlugin2 = usePlugin("react-router-dom");
6531
6537
  const theme2 = useTheme();
6532
6538
  const urlQuery = reactRouterDomPlugin2 ? useUrlQuery() : void 0;
6533
- const { colorTheme, tabsComponentState } = useBetterHtmlContextInternal();
6539
+ const { colorTheme, componentsState } = useBetterHtmlContextInternal();
6540
+ const firstRenderPassedRef = useRef5(false);
6534
6541
  const tabsRef = useRef5({});
6535
6542
  const [selectedTab, setSelectedTab] = useState9(() => {
6536
6543
  const selectedTabValue = tabs[0] ?? "";
@@ -6560,20 +6567,23 @@ var TabsComponent = function Tabs({
6560
6567
  );
6561
6568
  const leftSpacing = useMemo8(() => {
6562
6569
  const selectedTabIndex = tabs.findIndex((tab) => tab === selectedTab);
6563
- let totalWidth = 0;
6570
+ let spacing = 0;
6564
6571
  Object.values(tabsRef.current).forEach((tab, index) => {
6565
- if (index < selectedTabIndex) totalWidth += (tab?.getBoundingClientRect().width ?? 0) + tabsGap;
6572
+ if (index < selectedTabIndex) spacing += (tab?.getBoundingClientRect().width ?? 0) + tabsGap;
6566
6573
  });
6567
- return totalWidth;
6574
+ return spacing;
6568
6575
  }, [selectedTab, tabs, tabsGap]);
6569
6576
  useEffect9(() => {
6570
- const timeout = setTimeout(() => setRerenderState(Math.random()), 0.01 * 1e3);
6577
+ const timeout = setTimeout(() => {
6578
+ setRerenderState(Math.random());
6579
+ firstRenderPassedRef.current = true;
6580
+ }, 0.01 * 1e3);
6571
6581
  return () => {
6572
6582
  clearTimeout(timeout);
6573
6583
  };
6574
6584
  }, []);
6575
6585
  useEffect9(() => {
6576
- tabsComponentState.setTabGroups((oldValue) => {
6586
+ componentsState.tabs.setTabGroups((oldValue) => {
6577
6587
  const thisTabGroup = oldValue.find((item) => item.name === (name ?? defaultTabName));
6578
6588
  if (thisTabGroup) {
6579
6589
  return oldValue.map(
@@ -6593,6 +6603,12 @@ var TabsComponent = function Tabs({
6593
6603
  }
6594
6604
  });
6595
6605
  }, [selectedTab, name]);
6606
+ useEffect9(() => {
6607
+ tabsRef.current[selectedTab]?.scrollIntoView({
6608
+ behavior: firstRenderPassedRef.current ? "smooth" : void 0,
6609
+ block: "nearest"
6610
+ });
6611
+ }, [selectedTab]);
6596
6612
  return /* @__PURE__ */ jsxs16(Div_default.column, { width: "100%", gap: theme2.styles.space, ...props, children: [
6597
6613
  /* @__PURE__ */ jsxs16(Div_default, { position: "relative", className: "react-better-html-no-scrollbar", overflowY: "auto", children: [
6598
6614
  /* @__PURE__ */ jsx21(Div_default.row, { position: "relative", width: "fit-content", gap: tabsGap, userSelect: "none", children: tabs.map((tab) => {
@@ -6618,7 +6634,7 @@ var TabsComponent = function Tabs({
6618
6634
  tabsRef.current[tab] = ref;
6619
6635
  },
6620
6636
  children: [
6621
- tabsComponentState.tabsWithDots.includes(tab) && /* @__PURE__ */ jsx21(
6637
+ componentsState.tabs.tabsWithDots.includes(tab) && /* @__PURE__ */ jsx21(
6622
6638
  Div_default,
6623
6639
  {
6624
6640
  position: "absolute",
@@ -6655,7 +6671,7 @@ var TabsComponent = function Tabs({
6655
6671
  bottom: 0,
6656
6672
  left: leftSpacing,
6657
6673
  backgroundColor: accentColor ?? theme2.colors.primary,
6658
- transition: theme2.styles.transition
6674
+ transition: firstRenderPassedRef.current ? theme2.styles.transition : "none"
6659
6675
  }
6660
6676
  )
6661
6677
  ] }),
@@ -6663,16 +6679,16 @@ var TabsComponent = function Tabs({
6663
6679
  ] });
6664
6680
  };
6665
6681
  TabsComponent.content = function Content({ tab, tabWithDot, tabsGroupName, children }) {
6666
- const { tabsComponentState } = useBetterHtmlContextInternal();
6682
+ const { componentsState } = useBetterHtmlContextInternal();
6667
6683
  const thisTabGroupData = useMemo8(
6668
- () => tabsComponentState.tabGroups.find((item) => item.name === (tabsGroupName ?? defaultTabName)),
6669
- [tabsComponentState, tabsGroupName]
6684
+ () => componentsState.tabs.tabGroups.find((item) => item.name === (tabsGroupName ?? defaultTabName)),
6685
+ [componentsState.tabs, tabsGroupName]
6670
6686
  );
6671
6687
  useEffect9(() => {
6672
6688
  if (tabWithDot) {
6673
- tabsComponentState.setTabsWithDots?.((oldValue) => oldValue.includes(tab) ? oldValue : [...oldValue, tab]);
6689
+ componentsState.tabs.setTabsWithDots?.((oldValue) => oldValue.includes(tab) ? oldValue : [...oldValue, tab]);
6674
6690
  } else {
6675
- tabsComponentState.setTabsWithDots?.(
6691
+ componentsState.tabs.setTabsWithDots?.(
6676
6692
  (oldValue) => oldValue.includes(tab) ? oldValue.filter((tab2) => tab2 !== tab2) : oldValue
6677
6693
  );
6678
6694
  }
@@ -6686,7 +6702,8 @@ var Tabs_default = Tabs2;
6686
6702
  // src/components/Foldable.tsx
6687
6703
  import { forwardRef as forwardRef12, memo as memo22, useCallback as useCallback12, useEffect as useEffect10, useImperativeHandle as useImperativeHandle2, useRef as useRef6, useState as useState10 } from "react";
6688
6704
  import { jsx as jsx22, jsxs as jsxs17 } from "react/jsx-runtime";
6689
- var animationDuration = 0.2;
6705
+ var animationDurationClose = 0.15;
6706
+ var animationDurationOpen = animationDurationClose * 2;
6690
6707
  var FoldableComponent = forwardRef12(function Foldable({
6691
6708
  isOpen: controlledIsOpen,
6692
6709
  defaultOpen = false,
@@ -6703,7 +6720,7 @@ var FoldableComponent = forwardRef12(function Foldable({
6703
6720
  const theme2 = useTheme();
6704
6721
  const bodyRef = useRef6(null);
6705
6722
  const [internalIsOpen, setInternalIsOpen] = useBooleanState(defaultOpen);
6706
- const [bodyHeight, setBodyHeight] = useState10(defaultOpen ? void 0 : 0);
6723
+ const [bodyVirtualHeight, setBodyVirtualHeight] = useState10(500);
6707
6724
  const isOpen = controlledIsOpen !== void 0 ? controlledIsOpen : internalIsOpen;
6708
6725
  const open = useCallback12(() => {
6709
6726
  if (controlledIsOpen === void 0) setInternalIsOpen.setTrue();
@@ -6719,29 +6736,8 @@ var FoldableComponent = forwardRef12(function Foldable({
6719
6736
  }, [controlledIsOpen, isOpen, onOpenChange]);
6720
6737
  useEffect10(() => {
6721
6738
  if (!bodyRef.current) return;
6722
- if (isOpen) {
6723
- const height = bodyRef.current.scrollHeight;
6724
- setBodyHeight(height);
6725
- const timeout = setTimeout(() => {
6726
- setBodyHeight(void 0);
6727
- }, animationDuration * 1e3);
6728
- return () => {
6729
- clearTimeout(timeout);
6730
- };
6731
- } else {
6732
- if (bodyHeight === void 0) {
6733
- const height = bodyRef.current.scrollHeight;
6734
- setBodyHeight(height);
6735
- bodyRef.current.offsetHeight;
6736
- }
6737
- const timeout = setTimeout(() => {
6738
- setBodyHeight(0);
6739
- }, 0.01 * 1e3);
6740
- return () => {
6741
- clearTimeout(timeout);
6742
- };
6743
- }
6744
- }, [isOpen, animationDuration, bodyHeight]);
6739
+ setBodyVirtualHeight(Math.min(500, bodyRef.current.scrollHeight * 2));
6740
+ }, [isOpen]);
6745
6741
  useImperativeHandle2(
6746
6742
  ref,
6747
6743
  () => {
@@ -6754,7 +6750,7 @@ var FoldableComponent = forwardRef12(function Foldable({
6754
6750
  },
6755
6751
  [open, close, toggleOpen, isOpen]
6756
6752
  );
6757
- return /* @__PURE__ */ jsxs17(Div_default.column, { width: "100%", overflow: "hidden", ...props, children: [
6753
+ return /* @__PURE__ */ jsxs17(Div_default.column, { width: "100%", ...props, children: [
6758
6754
  renderHeader ? renderHeader(isOpen, toggleOpen) : /* @__PURE__ */ jsxs17(
6759
6755
  Div_default.row,
6760
6756
  {
@@ -6787,7 +6783,18 @@ var FoldableComponent = forwardRef12(function Foldable({
6787
6783
  }
6788
6784
  ),
6789
6785
  /* @__PURE__ */ jsx22(Div_default, { height: isOpen ? 1 : 0, opacity: isOpen ? 1 : 0, transition: theme2.styles.transition, children: /* @__PURE__ */ jsx22(Divider_default.horizontal, {}) }),
6790
- /* @__PURE__ */ jsx22(Div_default, { height: bodyHeight, transition: `height ${animationDuration}s ease`, ref: bodyRef, children: /* @__PURE__ */ jsx22(Div_default, { paddingBlock: theme2.styles.gap, paddingInline: headerPaddingInline, children }) })
6786
+ /* @__PURE__ */ jsx22(
6787
+ Div_default,
6788
+ {
6789
+ maxHeight: isOpen ? bodyVirtualHeight : 0,
6790
+ opacity: !isOpen ? 0 : void 0,
6791
+ transition: `max-height ${isOpen ? animationDurationOpen : animationDurationClose}s ease, opacity ${theme2.styles.transition}`,
6792
+ overflow: !isOpen ? "hidden" : void 0,
6793
+ pointerEvents: !isOpen ? "none" : void 0,
6794
+ ref: bodyRef,
6795
+ children: /* @__PURE__ */ jsx22(Div_default, { paddingBlock: theme2.styles.gap, paddingInline: headerPaddingInline, children })
6796
+ }
6797
+ )
6791
6798
  ] });
6792
6799
  });
6793
6800
  FoldableComponent.box = forwardRef12(function Box3({ ...props }, ref) {