xmlui 0.8.3 → 0.8.4

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.
@@ -37,17 +37,17 @@ exports.Heading = (0, react_1.forwardRef)(function Heading(_a, forwardedRef) {
37
37
  const anchorRef = (0, react_1.useRef)(null);
38
38
  const tableOfContentsContext = (0, react_1.useContext)(TableOfContentsContext_1.TableOfContentsContext);
39
39
  const registerHeading = tableOfContentsContext === null || tableOfContentsContext === void 0 ? void 0 : tableOfContentsContext.registerHeading;
40
- const observeIntersection = tableOfContentsContext === null || tableOfContentsContext === void 0 ? void 0 : tableOfContentsContext.hasTableOfContents;
41
40
  const ref = forwardedRef ? (0, react_compose_refs_1.composeRefs)(elementRef, forwardedRef) : elementRef;
42
41
  (0, react_1.useEffect)(() => {
43
42
  var _a, _b, _c, _d;
44
- if (observeIntersection && elementRef.current) {
43
+ if (elementRef.current) {
45
44
  const newAnchorId = (_d = (_c = (_b = (_a = elementRef.current.textContent) === null || _a === void 0 ? void 0 : _a.trim()) === null || _b === void 0 ? void 0 : _b.replace(/[^\w\s-]/g, "")) === null || _c === void 0 ? void 0 : _c.replace(/\s+/g, "-")) === null || _d === void 0 ? void 0 : _d.toLowerCase();
46
45
  setAnchorId(newAnchorId || null);
47
46
  }
48
- }, [observeIntersection]);
47
+ }, []);
49
48
  (0, hooks_1.useIsomorphicLayoutEffect)(() => {
50
- if (observeIntersection && elementRef.current && anchorId && !omitFromToc) {
49
+ if (elementRef.current && anchorId && !omitFromToc) {
50
+ console.log("HERE", { anchorId, registerHeading, level, omitFromToc });
51
51
  return registerHeading === null || registerHeading === void 0 ? void 0 : registerHeading({
52
52
  id: anchorId,
53
53
  level: parseInt(level.replace("h", "")),
@@ -55,10 +55,10 @@ exports.Heading = (0, react_1.forwardRef)(function Heading(_a, forwardedRef) {
55
55
  anchor: anchorRef.current,
56
56
  });
57
57
  }
58
- }, [anchorId, observeIntersection, registerHeading, level, omitFromToc]);
58
+ }, [anchorId, registerHeading, level, omitFromToc]);
59
59
  return ((0, jsx_runtime_1.jsxs)(Element, Object.assign({ ref: ref, id: uid, title: title, style: Object.assign(Object.assign(Object.assign({}, sx), style), (0, css_utils_1.getMaxLinesStyle)(maxLines)), className: (0, classnames_1.default)(Heading_module_scss_1.default.heading, Heading_module_scss_1.default[Element], className || "", {
60
60
  [Heading_module_scss_1.default.truncateOverflow]: maxLines > 0,
61
61
  [Heading_module_scss_1.default.preserveLinebreaks]: preserveLinebreaks,
62
62
  [Heading_module_scss_1.default.noEllipsis]: !ellipses,
63
- }) }, furtherProps, { children: [anchorId && observeIntersection && ((0, jsx_runtime_1.jsx)("span", { ref: anchorRef, id: anchorId, style: { width: 0, height: 0 }, "data-anchor": true })), children] })));
63
+ }) }, furtherProps, { children: [anchorId && ((0, jsx_runtime_1.jsx)("span", { ref: anchorRef, id: anchorId, style: { width: 0, height: 0 }, "data-anchor": true })), children] })));
64
64
  });
@@ -15,8 +15,8 @@ const TableOfContentsContext_1 = require("../../components-core/TableOfContentsC
15
15
  const hooks_1 = require("../../components-core/utils/hooks");
16
16
  exports.TableOfContents = (0, react_1.forwardRef)(function TableOfContents({ style, smoothScrolling, maxHeadingLevel = 6 }, forwardedRef) {
17
17
  const tocRef = (0, react_1.useRef)(null);
18
- const [activeAnchorId, setActiveId] = (0, react_1.useState)(null);
19
- const { headings, scrollToAnchor, subscribeToActiveAnchorChange } = (0, TableOfContentsContext_1.useTableOfContents)();
18
+ const { headings, scrollToAnchor, subscribeToActiveAnchorChange, activeAnchorId: initialActiveAnchorId } = (0, TableOfContentsContext_1.useTableOfContents)();
19
+ const [activeAnchorId, setActiveId] = (0, react_1.useState)(initialActiveAnchorId);
20
20
  (0, hooks_1.useIsomorphicLayoutEffect)(() => {
21
21
  return subscribeToActiveAnchorChange((id) => {
22
22
  const foundHeading = headings.find((heading) => heading.id === id);
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.httpMethodNames = exports.VariantPropsKeys = exports.variantOptionsMd = exports.TextVariantElement = exports.orderingValues = exports.scrollAnchoringNames = exports.scrollAnchoringValues = exports.validationStatusMd = exports.validationStatusNames = exports.validationStatusValues = exports.triggerPositionNames = exports.labelPositionMd = exports.labelPositionNames = exports.labelPositionValues = exports.placementMd = exports.placementNames = exports.statusColorMd = exports.statusColorNames = exports.iconPositionMd = exports.iconPositionNames = exports.iconPositionValues = exports.orientationOptionMd = exports.orientationOptionNames = exports.alignmentOptionMd = exports.alignmentOptionNames = exports.alignmentOptionValues = exports.buttonAriaNames = exports.buttonVariantMd = exports.buttonVariantNames = exports.buttonTypesMd = exports.buttonTypeNames = exports.buttonTypeValues = exports.buttonThemeMd = exports.buttonThemeNames = exports.sizeNames = exports.sizeMd = exports.viewportSizeNames = exports.viewportSizeMd = exports.LinkTargetMd = exports.LinkTargetNames = void 0;
3
+ exports.httpMethodNames = exports.VariantPropsKeys = exports.variantOptionsMd = exports.TextVariantElement = exports.orderingValues = exports.scrollAnchoringNames = exports.scrollAnchoringValues = exports.validationStatusMd = exports.validationStatusNames = exports.validationStatusValues = exports.triggerPositionNames = exports.labelPositionMd = exports.labelPositionNames = exports.labelPositionValues = exports.placementMd = exports.placementNames = exports.statusColorMd = exports.statusColorNames = exports.iconPositionMd = exports.iconPositionNames = exports.iconPositionValues = exports.orientationOptionMd = exports.orientationOptionNames = exports.alignmentOptionMd = exports.alignmentOptionNames = exports.alignmentOptionValues = exports.buttonAriaNames = exports.buttonVariantMd = exports.buttonVariantNames = exports.buttonVariantValues = exports.buttonTypesMd = exports.buttonTypeNames = exports.buttonTypeValues = exports.buttonThemeMd = exports.buttonThemeNames = exports.buttonThemeValues = exports.sizeNames = exports.sizeMd = exports.viewportSizeNames = exports.viewportSizeMd = exports.LinkTargetMd = exports.LinkTargetNames = void 0;
4
4
  exports.LinkTargetNames = ["_self", "_blank", "_parent", "_top", "_unfencedTop"];
5
5
  exports.LinkTargetMd = [
6
6
  {
@@ -47,8 +47,8 @@ exports.sizeMd = [
47
47
  const sizeValues = Object.keys(exports.sizeMd);
48
48
  exports.sizeNames = [...sizeValues];
49
49
  // --- Available button themes
50
- const buttonThemeValues = ["attention", "primary", "secondary"];
51
- exports.buttonThemeNames = [...buttonThemeValues];
50
+ exports.buttonThemeValues = ["attention", "primary", "secondary"];
51
+ exports.buttonThemeNames = [...exports.buttonThemeValues];
52
52
  exports.buttonThemeMd = [
53
53
  { value: "attention", description: "Attention state theme color" },
54
54
  { value: "primary", description: "Primary theme color" },
@@ -72,8 +72,8 @@ exports.buttonTypesMd = [
72
72
  },
73
73
  ];
74
74
  // --- Available button variants
75
- const buttonVariantValues = ["solid", "outlined", "ghost"];
76
- exports.buttonVariantNames = [...buttonVariantValues];
75
+ exports.buttonVariantValues = ["solid", "outlined", "ghost"];
76
+ exports.buttonVariantNames = [...exports.buttonVariantValues];
77
77
  exports.buttonVariantMd = [
78
78
  { value: "solid", description: "A button with a border and a filled background." },
79
79
  {
@@ -8,6 +8,7 @@ const react_1 = require("react");
8
8
  const hooks_1 = require("./utils/hooks");
9
9
  const react_2 = require("@remix-run/react");
10
10
  const constants_1 = require("./constants");
11
+ const AppContext_1 = require("./AppContext");
11
12
  /**
12
13
  * Several components work together to represent the hierarchy of a particular
13
14
  * app page as a TOC. This React component provides a context for storing this
@@ -22,7 +23,7 @@ function TableOfContentsProvider({ children }) {
22
23
  const [headings, setHeadings] = (0, react_1.useState)(constants_1.EMPTY_OBJECT);
23
24
  const [callbacks, setCallbacks] = (0, react_1.useState)(constants_1.EMPTY_ARRAY);
24
25
  const observer = (0, react_1.useRef)(null);
25
- const initialHeading = (0, react_1.useRef)(null);
26
+ const { forceRefreshAnchorScroll } = (0, AppContext_1.useAppContext)();
26
27
  const thisRef = (0, react_1.useRef)({
27
28
  suspendPositionBasedSetActiveId: false,
28
29
  });
@@ -32,8 +33,10 @@ function TableOfContentsProvider({ children }) {
32
33
  thisRef.current.suspendPositionBasedSetActiveId = false;
33
34
  }, []),
34
35
  });
36
+ const [activeAnchorId, setActiveAnchorId] = (0, react_1.useState)(null);
35
37
  const notify = (0, react_1.useCallback)((id) => {
36
38
  callbacks.forEach((cb) => cb(id));
39
+ setActiveAnchorId(id);
37
40
  }, [callbacks]);
38
41
  (0, react_1.useEffect)(() => {
39
42
  if (callbacks.length) {
@@ -75,6 +78,7 @@ function TableOfContentsProvider({ children }) {
75
78
  const scrollToAnchor = (0, react_1.useCallback)((id, smoothScrolling) => {
76
79
  const value = headings[id];
77
80
  if (value) {
81
+ thisRef.current.suspendPositionBasedSetActiveId = true;
78
82
  value.anchor.scrollIntoView({
79
83
  block: "start",
80
84
  inline: "start",
@@ -89,8 +93,13 @@ function TableOfContentsProvider({ children }) {
89
93
  preventHashScroll: true,
90
94
  },
91
95
  });
96
+ //we clear the preventHashScroll route state: https://stackoverflow.com/questions/72121228/how-to-update-location-state-in-react-router-v6
97
+ requestAnimationFrame(() => {
98
+ navigate({
99
+ hash: `#${value.id}`,
100
+ }, { replace: true });
101
+ });
92
102
  });
93
- thisRef.current.suspendPositionBasedSetActiveId = true;
94
103
  }
95
104
  }, [headings, navigate, notify]);
96
105
  const sortedHeadings = (0, react_1.useMemo)(() => {
@@ -104,26 +113,13 @@ function TableOfContentsProvider({ children }) {
104
113
  return -1;
105
114
  });
106
115
  }, [headings]);
107
- const location = (0, react_2.useLocation)();
116
+ //the content could take time to load, this way we try to force the scroll to anchor mechanism to kick in
117
+ const hasHeadings = sortedHeadings.length > 0;
108
118
  (0, react_1.useEffect)(() => {
109
- var _a;
110
- const hash = location.hash;
111
- if (hash) {
112
- if (initialHeading.current) {
113
- return;
114
- }
115
- else {
116
- initialHeading.current = (_a = sortedHeadings.find((value) => `#${value.id}` === hash)) === null || _a === void 0 ? void 0 : _a.anchor;
117
- if (initialHeading.current) {
118
- initialHeading.current.scrollIntoView({
119
- block: "start",
120
- inline: "start",
121
- behavior: "instant",
122
- });
123
- }
124
- }
119
+ if (hasHeadings) {
120
+ forceRefreshAnchorScroll();
125
121
  }
126
- }, [location.hash, sortedHeadings]);
122
+ }, [forceRefreshAnchorScroll, hasHeadings]);
127
123
  const subscribeToActiveAnchorChange = (0, react_1.useCallback)((cb) => {
128
124
  setCallbacks((prev) => {
129
125
  return [...prev, cb];
@@ -141,6 +137,7 @@ function TableOfContentsProvider({ children }) {
141
137
  scrollToAnchor,
142
138
  subscribeToActiveAnchorChange,
143
139
  hasTableOfContents: callbacks.length > 0,
140
+ activeAnchorId
144
141
  };
145
142
  }, [
146
143
  registerHeading,
@@ -148,6 +145,7 @@ function TableOfContentsProvider({ children }) {
148
145
  scrollToAnchor,
149
146
  subscribeToActiveAnchorChange,
150
147
  callbacks.length,
148
+ activeAnchorId,
151
149
  ]);
152
150
  return ((0, jsx_runtime_1.jsx)(exports.TableOfContentsContext.Provider, { value: contextValue, children: children }));
153
151
  }