@uncinc/uncinc-react-kitchen-sink 2.26.1 → 2.27.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.
@@ -0,0 +1,27 @@
1
+ import React from 'react';
2
+ import './style.scss';
3
+ export type ContentReadMarkerProps = {
4
+ title: string;
5
+ enabled?: boolean;
6
+ delay?: number;
7
+ resetKey?: React.Key;
8
+ event?: string;
9
+ className?: string;
10
+ };
11
+ /**
12
+ * ContentReadMarker
13
+ *
14
+ * Invisible marker to place after a page's main content. Pushes
15
+ * `{ event, title }` to the GTM dataLayer once, when the marker has been in
16
+ * view for `delay` ms.
17
+ *
18
+ * @param title - Sent along with the event.
19
+ * @param enabled - Wait for a condition first, e.g. the virtual page view.
20
+ * @param delay - Time in ms the marker must stay in view (default 1000),
21
+ * so scrolling past it (e.g. a scroll-to-top) does not count.
22
+ * @param resetKey - Change per page (e.g. node id) to track again; remounts
23
+ * the marker so a marker still in view does not fire early.
24
+ * @param event - Event name, defaults to `content_read`.
25
+ */
26
+ declare const ContentReadMarker: React.FC<ContentReadMarkerProps>;
27
+ export default ContentReadMarker;
@@ -0,0 +1,60 @@
1
+ var _excluded = ["resetKey"];
2
+ function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
3
+ function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
4
+ function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
5
+ import React, { useRef } from 'react';
6
+ import c from 'classnames';
7
+ import useInViewOnce from '../../hooks/useInViewOnce';
8
+ import './style.scss';
9
+ var Marker = function Marker(_ref) {
10
+ var title = _ref.title,
11
+ _ref$enabled = _ref.enabled,
12
+ enabled = _ref$enabled === void 0 ? true : _ref$enabled,
13
+ _ref$delay = _ref.delay,
14
+ delay = _ref$delay === void 0 ? 1000 : _ref$delay,
15
+ _ref$event = _ref.event,
16
+ event = _ref$event === void 0 ? 'content_read' : _ref$event,
17
+ className = _ref.className;
18
+ var ref = useRef(null);
19
+ useInViewOnce(ref, function () {
20
+ // Pushed directly, so GTM picks it up even if it loads later.
21
+ var w = window;
22
+ w.dataLayer = w.dataLayer || [];
23
+ w.dataLayer.push({
24
+ event: event,
25
+ title: title
26
+ });
27
+ }, {
28
+ enabled: enabled,
29
+ delay: delay
30
+ });
31
+ return /*#__PURE__*/React.createElement("div", {
32
+ ref: ref,
33
+ className: c('ContentReadMarker', className),
34
+ "aria-hidden": "true"
35
+ });
36
+ };
37
+
38
+ /**
39
+ * ContentReadMarker
40
+ *
41
+ * Invisible marker to place after a page's main content. Pushes
42
+ * `{ event, title }` to the GTM dataLayer once, when the marker has been in
43
+ * view for `delay` ms.
44
+ *
45
+ * @param title - Sent along with the event.
46
+ * @param enabled - Wait for a condition first, e.g. the virtual page view.
47
+ * @param delay - Time in ms the marker must stay in view (default 1000),
48
+ * so scrolling past it (e.g. a scroll-to-top) does not count.
49
+ * @param resetKey - Change per page (e.g. node id) to track again; remounts
50
+ * the marker so a marker still in view does not fire early.
51
+ * @param event - Event name, defaults to `content_read`.
52
+ */
53
+ var ContentReadMarker = function ContentReadMarker(_ref2) {
54
+ var resetKey = _ref2.resetKey,
55
+ props = _objectWithoutProperties(_ref2, _excluded);
56
+ return /*#__PURE__*/React.createElement(Marker, _extends({
57
+ key: resetKey
58
+ }, props));
59
+ };
60
+ export default ContentReadMarker;
@@ -0,0 +1,5 @@
1
+ .ContentReadMarker {
2
+ height: 1px;
3
+ margin-top: -1px;
4
+ pointer-events: none;
5
+ }
@@ -0,0 +1,23 @@
1
+ import { RefObject } from 'react';
2
+ type UseInViewOnceOptions = {
3
+ enabled?: boolean;
4
+ delay?: number;
5
+ observerOptions?: IntersectionObserverInit;
6
+ };
7
+ /**
8
+ * useInViewOnce
9
+ *
10
+ * Calls `callback` once, the first time `ref` is in view while `enabled` is
11
+ * true, and stays in view for `delay` ms. To fire again (e.g. on the next
12
+ * page), remount the element with a new `key`; that also restarts the
13
+ * observer.
14
+ *
15
+ * @param ref - The element to observe.
16
+ * @param callback - Called once when the element is in view.
17
+ * @param options - `enabled` (default true) delays firing until a condition
18
+ * is met. `delay` (default 0) is the time in ms the element
19
+ * must stay in view, so passing through it does not count.
20
+ * `observerOptions` must be a stable reference.
21
+ */
22
+ declare const useInViewOnce: (ref: RefObject<HTMLElement>, callback: () => void, { enabled, delay, observerOptions }?: UseInViewOnceOptions) => void;
23
+ export default useInViewOnce;
@@ -0,0 +1,45 @@
1
+ import { useEffect, useRef } from 'react';
2
+ import useOnScreen from './useOnScreen';
3
+ /**
4
+ * useInViewOnce
5
+ *
6
+ * Calls `callback` once, the first time `ref` is in view while `enabled` is
7
+ * true, and stays in view for `delay` ms. To fire again (e.g. on the next
8
+ * page), remount the element with a new `key`; that also restarts the
9
+ * observer.
10
+ *
11
+ * @param ref - The element to observe.
12
+ * @param callback - Called once when the element is in view.
13
+ * @param options - `enabled` (default true) delays firing until a condition
14
+ * is met. `delay` (default 0) is the time in ms the element
15
+ * must stay in view, so passing through it does not count.
16
+ * `observerOptions` must be a stable reference.
17
+ */
18
+ var useInViewOnce = function useInViewOnce(ref, callback) {
19
+ var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
20
+ _ref$enabled = _ref.enabled,
21
+ enabled = _ref$enabled === void 0 ? true : _ref$enabled,
22
+ _ref$delay = _ref.delay,
23
+ delay = _ref$delay === void 0 ? 0 : _ref$delay,
24
+ observerOptions = _ref.observerOptions;
25
+ var isOnScreen = useOnScreen(ref, observerOptions);
26
+ var hasFired = useRef(false);
27
+ var callbackRef = useRef(callback);
28
+ callbackRef.current = callback;
29
+ useEffect(function () {
30
+ if (!isOnScreen || !enabled || hasFired.current) return undefined;
31
+ var fire = function fire() {
32
+ hasFired.current = true;
33
+ callbackRef.current();
34
+ };
35
+ if (delay <= 0) {
36
+ fire();
37
+ return undefined;
38
+ }
39
+ var timeout = setTimeout(fire, delay);
40
+ return function () {
41
+ return clearTimeout(timeout);
42
+ };
43
+ }, [isOnScreen, enabled, delay]);
44
+ };
45
+ export default useInViewOnce;
package/dist/index.d.ts CHANGED
@@ -4,6 +4,7 @@ export { default as AccordionItem } from "./components/Accordion/AccordionItem";
4
4
  export { default as AccordionItemVariant } from "./components/Accordion/AccordionItemVariant";
5
5
  export { default as Button } from "./components/Button";
6
6
  export { default as AccessibleLinkDescription } from "./components/AccessibleLinkDescription";
7
+ export { default as ContentReadMarker } from "./components/ContentReadMarker";
7
8
  export { default as Hamburger } from "./components/Hamburger";
8
9
  export { default as Image } from "./components/Image";
9
10
  export { default as Icon } from "./components/Icon";
@@ -75,6 +76,7 @@ export { default as useBoolean } from "./hooks/useBoolean";
75
76
  export { default as useDialog } from "./hooks/useDialog";
76
77
  export { default as useDialogHelpers } from "./hooks/useDialogHelpers";
77
78
  export { default as useEscape } from "./hooks/useEscape";
79
+ export { default as useInViewOnce } from "./hooks/useInViewOnce";
78
80
  export { default as useOnScreen } from "./hooks/useOnScreen";
79
81
  export { default as usePrevious } from "./hooks/usePrevious";
80
82
  export { default as useScrollDepth } from "./hooks/useScrollDepth";
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ export { default as AccordionItem } from './components/Accordion/AccordionItem';
5
5
  export { default as AccordionItemVariant } from './components/Accordion/AccordionItemVariant';
6
6
  export { default as Button } from './components/Button';
7
7
  export { default as AccessibleLinkDescription } from './components/AccessibleLinkDescription';
8
+ export { default as ContentReadMarker } from './components/ContentReadMarker';
8
9
  export { default as Hamburger } from './components/Hamburger';
9
10
  export { default as Image } from './components/Image';
10
11
  export { default as Icon } from './components/Icon';
@@ -88,6 +89,7 @@ export { default as useBoolean } from './hooks/useBoolean';
88
89
  export { default as useDialog } from './hooks/useDialog';
89
90
  export { default as useDialogHelpers } from './hooks/useDialogHelpers';
90
91
  export { default as useEscape } from './hooks/useEscape';
92
+ export { default as useInViewOnce } from './hooks/useInViewOnce';
91
93
  export { default as useOnScreen } from './hooks/useOnScreen';
92
94
  export { default as usePrevious } from './hooks/usePrevious';
93
95
  export { default as useScrollDepth } from './hooks/useScrollDepth';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uncinc/uncinc-react-kitchen-sink",
3
- "version": "2.26.1",
3
+ "version": "2.27.0",
4
4
  "description": "Default React components that are easily extended.",
5
5
  "author": "uncinc",
6
6
  "license": "MIT",