@canonical/react-components 1.10.0 → 2.1.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.
- package/dist/components/Accordion/Accordion.d.ts +2 -2
- package/dist/components/Accordion/AccordionSection/AccordionSection.d.ts +2 -1
- package/dist/components/ActionButton/ActionButton.d.ts +2 -1
- package/dist/components/ArticlePagination/ArticlePagination.d.ts +2 -1
- package/dist/components/Badge/Badge.d.ts +2 -2
- package/dist/components/Button/Button.d.ts +2 -1
- package/dist/components/Card/Card.d.ts +2 -1
- package/dist/components/CheckboxInput/CheckableInput/CheckableInput.d.ts +2 -2
- package/dist/components/CheckboxInput/CheckboxInput.d.ts +2 -1
- package/dist/components/Chip/Chip.d.ts +2 -1
- package/dist/components/Code/Code.d.ts +2 -1
- package/dist/components/CodeSnippet/CodeSnippet.d.ts +2 -2
- package/dist/components/CodeSnippet/CodeSnippetBlock.d.ts +2 -1
- package/dist/components/CodeSnippet/CodeSnippetDropdown.d.ts +2 -1
- package/dist/components/Col/Col.d.ts +2 -1
- package/dist/components/ConfirmationButton/ConfirmationButton.d.ts +2 -2
- package/dist/components/ConfirmationButton/ConfirmationButton.js +2 -2
- package/dist/components/ConfirmationModal/ConfirmationModal.d.ts +2 -2
- package/dist/components/ContextualMenu/ContextualMenu.d.ts +1 -1
- package/dist/components/ContextualMenu/ContextualMenu.js +2 -2
- package/dist/components/ContextualMenu/ContextualMenuDropdown/ContextualMenuDropdown.d.ts +4 -4
- package/dist/components/CustomSelect/CustomSelect.d.ts +2 -1
- package/dist/components/EmptyState/EmptyState.d.ts +2 -2
- package/dist/components/Field/Field.d.ts +2 -1
- package/dist/components/Form/Form.d.ts +2 -1
- package/dist/components/FormikField/FormikField.d.ts +2 -1
- package/dist/components/Icon/Icon.d.ts +2 -1
- package/dist/components/Input/Input.d.ts +2 -1
- package/dist/components/Label/Label.d.ts +2 -1
- package/dist/components/Link/Link.d.ts +2 -1
- package/dist/components/List/List.d.ts +2 -1
- package/dist/components/Loader/Loader.d.ts +2 -1
- package/dist/components/MainTable/MainTable.d.ts +2 -1
- package/dist/components/Modal/Modal.d.ts +2 -2
- package/dist/components/ModularTable/ModularTable.d.ts +2 -2
- package/dist/components/MultiSelect/MultiSelect.js +1 -1
- package/dist/components/Navigation/Navigation.d.ts +2 -1
- package/dist/components/Navigation/Navigation.js +1 -1
- package/dist/components/Navigation/NavigationLink/NavigationLink.d.ts +2 -1
- package/dist/components/Navigation/NavigationMenu/NavigationMenu.d.ts +2 -1
- package/dist/components/Notification/Notification.d.ts +2 -2
- package/dist/components/Pagination/Pagination.d.ts +2 -1
- package/dist/components/Pagination/PaginationButton/PaginationButton.d.ts +2 -1
- package/dist/components/Pagination/PaginationItem/PaginationItem.d.ts +2 -1
- package/dist/components/RadioInput/RadioInput.d.ts +2 -1
- package/dist/components/Row/Row.d.ts +2 -1
- package/dist/components/SearchAndFilter/FilterPanelSection/FilterPanelSection.d.ts +2 -1
- package/dist/components/SearchAndFilter/SearchAndFilter.d.ts +2 -1
- package/dist/components/SearchAndFilter/SearchAndFilter.js +1 -1
- package/dist/components/SearchBox/SearchBox.d.ts +0 -4
- package/dist/components/SearchBox/SearchBox.js +1 -1
- package/dist/components/Select/Select.d.ts +2 -1
- package/dist/components/SkipLink/SkipLink.d.ts +2 -2
- package/dist/components/Slider/Slider.d.ts +2 -1
- package/dist/components/Spinner/Spinner.d.ts +2 -2
- package/dist/components/StatusLabel/StatusLabel.d.ts +2 -1
- package/dist/components/Stepper/Step/Step.d.ts +2 -1
- package/dist/components/Stepper/Stepper.d.ts +3 -3
- package/dist/components/Strip/Strip.d.ts +2 -1
- package/dist/components/SummaryButton/SummaryButton.d.ts +2 -1
- package/dist/components/Switch/Switch.d.ts +2 -1
- package/dist/components/Table/Table.d.ts +2 -2
- package/dist/components/TableCell/TableCell.d.ts +2 -2
- package/dist/components/TableHeader/TableHeader.d.ts +2 -2
- package/dist/components/TablePagination/TablePaginationControls/TablePaginationControls.d.ts +2 -2
- package/dist/components/TablePagination/utils.d.ts +3 -3
- package/dist/components/TableRow/TableRow.d.ts +2 -1
- package/dist/components/Tabs/Tabs.d.ts +2 -1
- package/dist/components/Textarea/Textarea.d.ts +2 -1
- package/dist/components/Tooltip/Tooltip.d.ts +2 -1
- package/dist/components/Tooltip/Tooltip.js +2 -2
- package/dist/esm/components/Accordion/Accordion.d.ts +2 -2
- package/dist/esm/components/Accordion/AccordionSection/AccordionSection.d.ts +2 -1
- package/dist/esm/components/ActionButton/ActionButton.d.ts +2 -1
- package/dist/esm/components/ArticlePagination/ArticlePagination.d.ts +2 -1
- package/dist/esm/components/Badge/Badge.d.ts +2 -2
- package/dist/esm/components/Button/Button.d.ts +2 -1
- package/dist/esm/components/Card/Card.d.ts +2 -1
- package/dist/esm/components/CheckboxInput/CheckableInput/CheckableInput.d.ts +2 -2
- package/dist/esm/components/CheckboxInput/CheckboxInput.d.ts +2 -1
- package/dist/esm/components/Chip/Chip.d.ts +2 -1
- package/dist/esm/components/Code/Code.d.ts +2 -1
- package/dist/esm/components/CodeSnippet/CodeSnippet.d.ts +2 -2
- package/dist/esm/components/CodeSnippet/CodeSnippetBlock.d.ts +2 -1
- package/dist/esm/components/CodeSnippet/CodeSnippetDropdown.d.ts +2 -1
- package/dist/esm/components/Col/Col.d.ts +2 -1
- package/dist/esm/components/ConfirmationButton/ConfirmationButton.d.ts +2 -2
- package/dist/esm/components/ConfirmationButton/ConfirmationButton.js +1 -1
- package/dist/esm/components/ConfirmationModal/ConfirmationModal.d.ts +2 -2
- package/dist/esm/components/ContextualMenu/ContextualMenu.d.ts +1 -1
- package/dist/esm/components/ContextualMenu/ContextualMenu.js +1 -1
- package/dist/esm/components/ContextualMenu/ContextualMenuDropdown/ContextualMenuDropdown.d.ts +4 -4
- package/dist/esm/components/CustomSelect/CustomSelect.d.ts +2 -1
- package/dist/esm/components/EmptyState/EmptyState.d.ts +2 -2
- package/dist/esm/components/Field/Field.d.ts +2 -1
- package/dist/esm/components/Form/Form.d.ts +2 -1
- package/dist/esm/components/FormikField/FormikField.d.ts +2 -1
- package/dist/esm/components/Icon/Icon.d.ts +2 -1
- package/dist/esm/components/Input/Input.d.ts +2 -1
- package/dist/esm/components/Label/Label.d.ts +2 -1
- package/dist/esm/components/Link/Link.d.ts +2 -1
- package/dist/esm/components/List/List.d.ts +2 -1
- package/dist/esm/components/Loader/Loader.d.ts +2 -1
- package/dist/esm/components/MainTable/MainTable.d.ts +2 -1
- package/dist/esm/components/Modal/Modal.d.ts +2 -2
- package/dist/esm/components/ModularTable/ModularTable.d.ts +2 -2
- package/dist/esm/components/MultiSelect/MultiSelect.js +1 -1
- package/dist/esm/components/Navigation/Navigation.d.ts +2 -1
- package/dist/esm/components/Navigation/Navigation.js +1 -1
- package/dist/esm/components/Navigation/NavigationLink/NavigationLink.d.ts +2 -1
- package/dist/esm/components/Navigation/NavigationMenu/NavigationMenu.d.ts +2 -1
- package/dist/esm/components/Notification/Notification.d.ts +2 -2
- package/dist/esm/components/Pagination/Pagination.d.ts +2 -1
- package/dist/esm/components/Pagination/PaginationButton/PaginationButton.d.ts +2 -1
- package/dist/esm/components/Pagination/PaginationItem/PaginationItem.d.ts +2 -1
- package/dist/esm/components/RadioInput/RadioInput.d.ts +2 -1
- package/dist/esm/components/Row/Row.d.ts +2 -1
- package/dist/esm/components/SearchAndFilter/FilterPanelSection/FilterPanelSection.d.ts +2 -1
- package/dist/esm/components/SearchAndFilter/SearchAndFilter.d.ts +2 -1
- package/dist/esm/components/SearchAndFilter/SearchAndFilter.js +1 -1
- package/dist/esm/components/SearchBox/SearchBox.d.ts +0 -4
- package/dist/esm/components/SearchBox/SearchBox.js +1 -1
- package/dist/esm/components/Select/Select.d.ts +2 -1
- package/dist/esm/components/SkipLink/SkipLink.d.ts +2 -2
- package/dist/esm/components/Slider/Slider.d.ts +2 -1
- package/dist/esm/components/Spinner/Spinner.d.ts +2 -2
- package/dist/esm/components/StatusLabel/StatusLabel.d.ts +2 -1
- package/dist/esm/components/Stepper/Step/Step.d.ts +2 -1
- package/dist/esm/components/Stepper/Stepper.d.ts +3 -3
- package/dist/esm/components/Strip/Strip.d.ts +2 -1
- package/dist/esm/components/SummaryButton/SummaryButton.d.ts +2 -1
- package/dist/esm/components/Switch/Switch.d.ts +2 -1
- package/dist/esm/components/Table/Table.d.ts +2 -2
- package/dist/esm/components/TableCell/TableCell.d.ts +2 -2
- package/dist/esm/components/TableHeader/TableHeader.d.ts +2 -2
- package/dist/esm/components/TablePagination/TablePaginationControls/TablePaginationControls.d.ts +2 -2
- package/dist/esm/components/TablePagination/utils.d.ts +3 -3
- package/dist/esm/components/TableRow/TableRow.d.ts +2 -1
- package/dist/esm/components/Tabs/Tabs.d.ts +2 -1
- package/dist/esm/components/Textarea/Textarea.d.ts +2 -1
- package/dist/esm/components/Tooltip/Tooltip.d.ts +2 -1
- package/dist/esm/components/Tooltip/Tooltip.js +1 -1
- package/dist/esm/external/index.d.ts +2 -0
- package/dist/esm/external/index.js +1 -0
- package/dist/esm/external/usePortal.d.ts +39 -0
- package/dist/esm/external/usePortal.js +125 -0
- package/dist/esm/external/usePortal.test.d.ts +5 -0
- package/dist/esm/external/useSSR.d.ts +19 -0
- package/dist/esm/external/useSSR.js +43 -0
- package/dist/esm/hooks/useThrottle.js +2 -2
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -1
- package/dist/external/index.d.ts +2 -0
- package/dist/external/index.js +12 -0
- package/dist/external/usePortal.d.ts +39 -0
- package/dist/external/usePortal.js +133 -0
- package/dist/external/usePortal.test.d.ts +5 -0
- package/dist/external/useSSR.d.ts +19 -0
- package/dist/external/useSSR.js +48 -0
- package/dist/hooks/useThrottle.js +2 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +9 -1
- package/package.json +16 -24
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { HTMLProps, ReactNode } from "react";
|
|
2
|
+
import React from "react";
|
|
2
3
|
import type { PropsWithSpread } from "../../types";
|
|
3
4
|
export type Props = PropsWithSpread<{
|
|
4
5
|
/**
|
|
@@ -6,5 +7,5 @@ export type Props = PropsWithSpread<{
|
|
|
6
7
|
*/
|
|
7
8
|
children: ReactNode;
|
|
8
9
|
}, HTMLProps<HTMLTableRowElement>>;
|
|
9
|
-
declare const TableRow: ({ children, ...trProps }: Props) => JSX.Element;
|
|
10
|
+
declare const TableRow: ({ children, ...trProps }: Props) => React.JSX.Element;
|
|
10
11
|
export default TableRow;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import React from "react";
|
|
1
2
|
import type { HTMLProps, ElementType, ReactNode, ComponentType } from "react";
|
|
2
3
|
import type { ClassName } from "../../types";
|
|
3
4
|
export type TabLink<P = null> = {
|
|
@@ -41,5 +42,5 @@ export type Props<P = null> = {
|
|
|
41
42
|
Tabs organise and allow navigation between groups of content that are related and at the same level
|
|
42
43
|
of hierarchy.
|
|
43
44
|
*/
|
|
44
|
-
declare const Tabs: <P>({ className, links, listClassName, }: Props<P>) => JSX.Element;
|
|
45
|
+
declare const Tabs: <P>({ className, links, listClassName, }: Props<P>) => React.JSX.Element;
|
|
45
46
|
export default Tabs;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import React from "react";
|
|
1
2
|
import type { TextareaHTMLAttributes, ReactNode } from "react";
|
|
2
3
|
import type { ClassName, PropsWithSpread } from "../../types";
|
|
3
4
|
/**
|
|
@@ -66,5 +67,5 @@ export type Props = PropsWithSpread<{
|
|
|
66
67
|
*
|
|
67
68
|
* The Textarea component defines a multi-line text input control.
|
|
68
69
|
*/
|
|
69
|
-
declare const Textarea: ({ caution, className, error, grow, help, id, label, labelClassName, onKeyUp, onControlEnter, required, stacked, style, success, takeFocus, wrapperClassName, ...props }: Props) => JSX.Element;
|
|
70
|
+
declare const Textarea: ({ caution, className, error, grow, help, id, label, labelClassName, onKeyUp, onControlEnter, required, stacked, style, success, takeFocus, wrapperClassName, ...props }: Props) => React.JSX.Element;
|
|
70
71
|
export default Textarea;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import React from "react";
|
|
1
2
|
import type { ReactNode } from "react";
|
|
2
3
|
import type { WindowFitment } from "../../hooks";
|
|
3
4
|
import type { ClassName, ValueOf } from "../../types";
|
|
@@ -71,5 +72,5 @@ export declare const adjustForWindow: (position: Position, fitsWindow: WindowFit
|
|
|
71
72
|
*
|
|
72
73
|
* An alternative use of tooltips is to provide information on a disabled actionable element, e.g. for disabled buttons, providing information about why they are disabled.
|
|
73
74
|
*/
|
|
74
|
-
declare const Tooltip: ({ autoAdjust, children, className, followMouse, message, position, positionElementClassName, tooltipClassName, zIndex, delay, }: Props) => JSX.Element;
|
|
75
|
+
declare const Tooltip: ({ autoAdjust, children, className, followMouse, message, position, positionElementClassName, tooltipClassName, zIndex, delay, }: Props) => React.JSX.Element;
|
|
75
76
|
export default Tooltip;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import classNames from "classnames";
|
|
2
2
|
import React, { useCallback, useEffect, useId, useRef, useState } from "react";
|
|
3
|
-
import usePortal from "react-useportal";
|
|
4
3
|
import { useWindowFitment, useListener } from "../../hooks";
|
|
4
|
+
import { usePortal } from "../../external";
|
|
5
5
|
export var position = {
|
|
6
6
|
btmCenter: "btm-center",
|
|
7
7
|
btmLeft: "btm-left",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { usePortal } from "./usePortal";
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is a reference implementation of the usePortal hook from react-useportal: https://github.com/iamthesiz/react-useportal/blob/master/usePortal.ts
|
|
3
|
+
* The license for the content in this file is goverened by the original project's license: https://github.com/iamthesiz/react-useportal/blob/master/license.md
|
|
4
|
+
*/
|
|
5
|
+
import { ReactNode, SyntheticEvent, RefObject } from "react";
|
|
6
|
+
type CustomEvent<T = HTMLElement> = {
|
|
7
|
+
event?: SyntheticEvent<T, Event>;
|
|
8
|
+
portal: RefObject<HTMLElement>;
|
|
9
|
+
targetEl: RefObject<HTMLElement>;
|
|
10
|
+
} & SyntheticEvent<T, Event>;
|
|
11
|
+
type CustomEventHandler<T = HTMLElement> = (customEvent: CustomEvent<T>) => void;
|
|
12
|
+
export type UsePortalOptions = {
|
|
13
|
+
closeOnOutsideClick?: boolean;
|
|
14
|
+
closeOnEsc?: boolean;
|
|
15
|
+
bindTo?: RefObject<HTMLElement>;
|
|
16
|
+
isOpen?: boolean;
|
|
17
|
+
onOpen?: CustomEventHandler;
|
|
18
|
+
onClose?: CustomEventHandler;
|
|
19
|
+
onPortalClick?: CustomEventHandler;
|
|
20
|
+
programmaticallyOpen?: boolean;
|
|
21
|
+
};
|
|
22
|
+
export declare const errorMessage1 = "You must either add a `ref` to the element you are interacting with or pass an `event` to openPortal(e) or togglePortal(e) when the `programmaticallyOpen` option is not set to `true`.";
|
|
23
|
+
export declare const usePortal: ({ closeOnOutsideClick, closeOnEsc, bindTo, isOpen: defaultIsOpen, onOpen, onClose, onPortalClick, programmaticallyOpen, }?: UsePortalOptions) => (boolean | RefObject<HTMLElement> | ((e?: SyntheticEvent<HTMLElement, Event>) => void) | (({ children }: {
|
|
24
|
+
children: ReactNode;
|
|
25
|
+
}) => import("react").JSX.Element))[] & {
|
|
26
|
+
isOpen: boolean;
|
|
27
|
+
openPortal: (e?: SyntheticEvent<HTMLElement, Event>) => void;
|
|
28
|
+
ref: RefObject<HTMLElement>;
|
|
29
|
+
closePortal: (e?: SyntheticEvent<HTMLElement, Event>) => void;
|
|
30
|
+
togglePortal: (e?: SyntheticEvent<HTMLElement, Event>) => void;
|
|
31
|
+
Portal: ({ children }: {
|
|
32
|
+
children: ReactNode;
|
|
33
|
+
}) => import("react").JSX.Element;
|
|
34
|
+
portalRef: RefObject<HTMLElement>;
|
|
35
|
+
bind: {
|
|
36
|
+
ref: RefObject<HTMLElement>;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is a reference implementation of the usePortal hook from react-useportal: https://github.com/iamthesiz/react-useportal/blob/master/usePortal.ts
|
|
3
|
+
* The license for the content in this file is goverened by the original project's license: https://github.com/iamthesiz/react-useportal/blob/master/license.md
|
|
4
|
+
*/
|
|
5
|
+
import { useState, useRef, useEffect, useCallback, useMemo } from "react";
|
|
6
|
+
import { createPortal } from "react-dom";
|
|
7
|
+
import { useSSR } from "./useSSR";
|
|
8
|
+
export var errorMessage1 = "You must either add a `ref` to the element you are interacting with or pass an `event` to openPortal(e) or togglePortal(e) when the `programmaticallyOpen` option is not set to `true`.";
|
|
9
|
+
export var usePortal = function usePortal() {
|
|
10
|
+
var {
|
|
11
|
+
closeOnOutsideClick = true,
|
|
12
|
+
closeOnEsc = true,
|
|
13
|
+
bindTo,
|
|
14
|
+
// attach the portal to this node in the DOM
|
|
15
|
+
isOpen: defaultIsOpen = false,
|
|
16
|
+
onOpen,
|
|
17
|
+
onClose,
|
|
18
|
+
onPortalClick,
|
|
19
|
+
programmaticallyOpen = false
|
|
20
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
21
|
+
var {
|
|
22
|
+
isServer,
|
|
23
|
+
isBrowser
|
|
24
|
+
} = useSSR();
|
|
25
|
+
var [isOpen, makeOpen] = useState(defaultIsOpen);
|
|
26
|
+
// we use this ref because `isOpen` is stale for handleOutsideMouseClick
|
|
27
|
+
var open = useRef(isOpen);
|
|
28
|
+
var setOpen = useCallback(v => {
|
|
29
|
+
// workaround to not have stale `isOpen` in the handleOutsideMouseClick
|
|
30
|
+
open.current = v;
|
|
31
|
+
makeOpen(v);
|
|
32
|
+
}, []);
|
|
33
|
+
var targetEl = useRef(null); // this is the element you are clicking/hovering/whatever, to trigger opening the portal
|
|
34
|
+
var portal = useRef(isBrowser ? document.createElement("div") : null);
|
|
35
|
+
useEffect(() => {
|
|
36
|
+
if (isBrowser && !portal.current) portal.current = document.createElement("div");
|
|
37
|
+
}, [isBrowser, portal]);
|
|
38
|
+
var elToMountTo = useMemo(() => {
|
|
39
|
+
if (isServer) return null;
|
|
40
|
+
return bindTo && bindTo.current || document.body;
|
|
41
|
+
}, [isServer, bindTo]);
|
|
42
|
+
var createCustomEvent = e => {
|
|
43
|
+
if (!e) return {
|
|
44
|
+
portal,
|
|
45
|
+
targetEl,
|
|
46
|
+
event: e
|
|
47
|
+
};
|
|
48
|
+
var event = e || {};
|
|
49
|
+
if (event.persist) event.persist();
|
|
50
|
+
event.portal = portal;
|
|
51
|
+
event.targetEl = targetEl;
|
|
52
|
+
event.event = e;
|
|
53
|
+
var {
|
|
54
|
+
currentTarget
|
|
55
|
+
} = e;
|
|
56
|
+
if (!targetEl.current && currentTarget && currentTarget !== document) targetEl.current = event.currentTarget;
|
|
57
|
+
return event;
|
|
58
|
+
};
|
|
59
|
+
var openPortal = useCallback(e => {
|
|
60
|
+
if (isServer) return;
|
|
61
|
+
var customEvent = createCustomEvent(e);
|
|
62
|
+
// for some reason, when we don't have the event argument, there
|
|
63
|
+
// is a weird race condition. Would like to see if we can remove
|
|
64
|
+
// setTimeout, but for now this works
|
|
65
|
+
if (targetEl.current == null && !programmaticallyOpen) {
|
|
66
|
+
setTimeout(() => setOpen(true), 0);
|
|
67
|
+
throw Error(errorMessage1);
|
|
68
|
+
}
|
|
69
|
+
if (onOpen) onOpen(customEvent);
|
|
70
|
+
setOpen(true);
|
|
71
|
+
}, [isServer, portal, setOpen, targetEl, onOpen, programmaticallyOpen]);
|
|
72
|
+
var closePortal = useCallback(e => {
|
|
73
|
+
if (isServer) return;
|
|
74
|
+
var customEvent = createCustomEvent(e);
|
|
75
|
+
if (onClose && open.current) onClose(customEvent);
|
|
76
|
+
if (open.current) setOpen(false);
|
|
77
|
+
}, [isServer, onClose, setOpen]);
|
|
78
|
+
var togglePortal = useCallback(e => open.current ? closePortal(e) : openPortal(e), [closePortal, openPortal]);
|
|
79
|
+
var handleKeydown = useCallback(e => e.key === "Escape" && closeOnEsc ? closePortal(e) : undefined, [closeOnEsc, closePortal]);
|
|
80
|
+
var handleOutsideMouseClick = useCallback(e => {
|
|
81
|
+
var containsTarget = target => target.current.contains(e.target);
|
|
82
|
+
// There might not be a targetEl if the portal was opened programmatically.
|
|
83
|
+
if (containsTarget(portal) || e.button !== 0 || !open.current || targetEl.current && containsTarget(targetEl)) return;
|
|
84
|
+
if (closeOnOutsideClick) closePortal(e);
|
|
85
|
+
}, [isServer, closePortal, closeOnOutsideClick, portal]);
|
|
86
|
+
var handleMouseDown = useCallback(e => {
|
|
87
|
+
if (isServer || !(portal.current instanceof HTMLElement)) return;
|
|
88
|
+
var customEvent = createCustomEvent(e);
|
|
89
|
+
if (portal.current.contains(customEvent.target) && onPortalClick) onPortalClick(customEvent);
|
|
90
|
+
handleOutsideMouseClick(e);
|
|
91
|
+
}, [handleOutsideMouseClick, isServer]);
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
if (isServer) return null;
|
|
94
|
+
if (!(elToMountTo instanceof HTMLElement) || !(portal.current instanceof HTMLElement)) return null;
|
|
95
|
+
var node = portal.current;
|
|
96
|
+
elToMountTo.appendChild(portal.current);
|
|
97
|
+
document.addEventListener("keydown", handleKeydown);
|
|
98
|
+
document.addEventListener("mousedown", handleMouseDown);
|
|
99
|
+
return () => {
|
|
100
|
+
document.removeEventListener("keydown", handleKeydown);
|
|
101
|
+
document.removeEventListener("mousedown", handleMouseDown);
|
|
102
|
+
elToMountTo.removeChild(node);
|
|
103
|
+
};
|
|
104
|
+
}, [isServer, handleOutsideMouseClick, handleKeydown, elToMountTo, portal]);
|
|
105
|
+
var Portal = useCallback(_ref => {
|
|
106
|
+
var {
|
|
107
|
+
children
|
|
108
|
+
} = _ref;
|
|
109
|
+
if (portal.current != null) return /*#__PURE__*/createPortal(children, portal.current);
|
|
110
|
+
return null;
|
|
111
|
+
}, [portal]);
|
|
112
|
+
return Object.assign([openPortal, closePortal, open.current, Portal, togglePortal, targetEl, portal], {
|
|
113
|
+
isOpen: open.current,
|
|
114
|
+
openPortal,
|
|
115
|
+
ref: targetEl,
|
|
116
|
+
closePortal,
|
|
117
|
+
togglePortal,
|
|
118
|
+
Portal,
|
|
119
|
+
portalRef: portal,
|
|
120
|
+
bind: {
|
|
121
|
+
// used if you want to spread all html attributes onto the target element
|
|
122
|
+
ref: targetEl
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
};
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is a reference implementation of the usePortal hook from react-useportal: https://github.com/iamthesiz/react-useportal/blob/master/usePortal.test.ts
|
|
3
|
+
* The license for the content in this file is goverened by the original project's license: https://github.com/iamthesiz/react-useportal/blob/master/license.md
|
|
4
|
+
*/
|
|
5
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is a reference implementation of the useSSR hook from use-ssr: https://github.com/iamthesiz/use-ssr/blob/master/useSSR.ts
|
|
3
|
+
* The license for the content in this file is goverened by the original project's license: https://github.com/iamthesiz/use-ssr/blob/master/license.md
|
|
4
|
+
*/
|
|
5
|
+
interface UseSSRReturn {
|
|
6
|
+
isBrowser: boolean;
|
|
7
|
+
isServer: boolean;
|
|
8
|
+
device: Device;
|
|
9
|
+
canUseWorkers: boolean;
|
|
10
|
+
canUseEventListeners: boolean;
|
|
11
|
+
canUseViewport: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare enum Device {
|
|
14
|
+
Browser = "browser",
|
|
15
|
+
Server = "server"
|
|
16
|
+
}
|
|
17
|
+
export declare const weAreServer: () => void;
|
|
18
|
+
export declare const useSSR: () => UseSSRReturn;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
2
|
+
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
3
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
4
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : String(i); }
|
|
5
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
6
|
+
/**
|
|
7
|
+
* This is a reference implementation of the useSSR hook from use-ssr: https://github.com/iamthesiz/use-ssr/blob/master/useSSR.ts
|
|
8
|
+
* The license for the content in this file is goverened by the original project's license: https://github.com/iamthesiz/use-ssr/blob/master/license.md
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export var Device = /*#__PURE__*/function (Device) {
|
|
12
|
+
Device["Browser"] = "browser";
|
|
13
|
+
Device["Server"] = "server";
|
|
14
|
+
return Device;
|
|
15
|
+
}({});
|
|
16
|
+
var {
|
|
17
|
+
Browser,
|
|
18
|
+
Server
|
|
19
|
+
} = Device;
|
|
20
|
+
var canUseDOM = !!(typeof window !== "undefined" && window.document && window.document.createElement);
|
|
21
|
+
var device = canUseDOM ? Browser : Server;
|
|
22
|
+
var SSRObject = {
|
|
23
|
+
isBrowser: device === Browser,
|
|
24
|
+
isServer: device === Server,
|
|
25
|
+
device,
|
|
26
|
+
canUseWorkers: typeof Worker !== "undefined",
|
|
27
|
+
canUseEventListeners: device === Browser && !!window.addEventListener,
|
|
28
|
+
canUseViewport: device === Browser && !!window.screen
|
|
29
|
+
};
|
|
30
|
+
var assign = function assign() {
|
|
31
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
32
|
+
args[_key] = arguments[_key];
|
|
33
|
+
}
|
|
34
|
+
return args.reduce((acc, obj) => _objectSpread(_objectSpread({}, acc), obj), {});
|
|
35
|
+
};
|
|
36
|
+
var values = obj => Object.keys(obj).map(key => obj[key]);
|
|
37
|
+
var toArrayObject = () => assign((values(SSRObject), SSRObject));
|
|
38
|
+
var useSSRObject = toArrayObject();
|
|
39
|
+
export var weAreServer = () => {
|
|
40
|
+
SSRObject.isServer = true;
|
|
41
|
+
useSSRObject = toArrayObject();
|
|
42
|
+
};
|
|
43
|
+
export var useSSR = () => useSSRObject;
|
|
@@ -10,8 +10,8 @@ export var THROTTLE_DELAY = 1000 / 60;
|
|
|
10
10
|
*/
|
|
11
11
|
export var useThrottle = function useThrottle(callback) {
|
|
12
12
|
var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : THROTTLE_DELAY;
|
|
13
|
-
var timeout = useRef();
|
|
14
|
-
var lastCall = useRef();
|
|
13
|
+
var timeout = useRef(null);
|
|
14
|
+
var lastCall = useRef(null);
|
|
15
15
|
var lastArgs = useRef(null);
|
|
16
16
|
var throttle = useCallback(function () {
|
|
17
17
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -138,3 +138,5 @@ export type { WindowFitment } from "./hooks";
|
|
|
138
138
|
export { isNavigationAnchor, isNavigationButton } from "./utils";
|
|
139
139
|
export type { ClassName, Headings, PropsWithSpread, SortDirection, SubComponentProps, TSFixMe, ValueOf, } from "./types";
|
|
140
140
|
export { Theme } from "./enums";
|
|
141
|
+
export type { UsePortalOptions } from "./external";
|
|
142
|
+
export { usePortal } from "./external";
|
package/dist/esm/index.js
CHANGED
|
@@ -70,4 +70,5 @@ export { default as TablePaginationControls } from "./components/TablePagination
|
|
|
70
70
|
export { default as CustomSelect } from "./components/CustomSelect";
|
|
71
71
|
export { useOnClickOutside, useClickOutside, useId, useListener, useOnEscapePressed, usePagination, usePrevious, useThrottle, useWindowFitment } from "./hooks";
|
|
72
72
|
export { isNavigationAnchor, isNavigationButton } from "./utils";
|
|
73
|
-
export { Theme } from "./enums";
|
|
73
|
+
export { Theme } from "./enums";
|
|
74
|
+
export { usePortal } from "./external";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "usePortal", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _usePortal.usePortal;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
var _usePortal = require("./usePortal");
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is a reference implementation of the usePortal hook from react-useportal: https://github.com/iamthesiz/react-useportal/blob/master/usePortal.ts
|
|
3
|
+
* The license for the content in this file is goverened by the original project's license: https://github.com/iamthesiz/react-useportal/blob/master/license.md
|
|
4
|
+
*/
|
|
5
|
+
import { ReactNode, SyntheticEvent, RefObject } from "react";
|
|
6
|
+
type CustomEvent<T = HTMLElement> = {
|
|
7
|
+
event?: SyntheticEvent<T, Event>;
|
|
8
|
+
portal: RefObject<HTMLElement>;
|
|
9
|
+
targetEl: RefObject<HTMLElement>;
|
|
10
|
+
} & SyntheticEvent<T, Event>;
|
|
11
|
+
type CustomEventHandler<T = HTMLElement> = (customEvent: CustomEvent<T>) => void;
|
|
12
|
+
export type UsePortalOptions = {
|
|
13
|
+
closeOnOutsideClick?: boolean;
|
|
14
|
+
closeOnEsc?: boolean;
|
|
15
|
+
bindTo?: RefObject<HTMLElement>;
|
|
16
|
+
isOpen?: boolean;
|
|
17
|
+
onOpen?: CustomEventHandler;
|
|
18
|
+
onClose?: CustomEventHandler;
|
|
19
|
+
onPortalClick?: CustomEventHandler;
|
|
20
|
+
programmaticallyOpen?: boolean;
|
|
21
|
+
};
|
|
22
|
+
export declare const errorMessage1 = "You must either add a `ref` to the element you are interacting with or pass an `event` to openPortal(e) or togglePortal(e) when the `programmaticallyOpen` option is not set to `true`.";
|
|
23
|
+
export declare const usePortal: ({ closeOnOutsideClick, closeOnEsc, bindTo, isOpen: defaultIsOpen, onOpen, onClose, onPortalClick, programmaticallyOpen, }?: UsePortalOptions) => (boolean | RefObject<HTMLElement> | ((e?: SyntheticEvent<HTMLElement, Event>) => void) | (({ children }: {
|
|
24
|
+
children: ReactNode;
|
|
25
|
+
}) => import("react").JSX.Element))[] & {
|
|
26
|
+
isOpen: boolean;
|
|
27
|
+
openPortal: (e?: SyntheticEvent<HTMLElement, Event>) => void;
|
|
28
|
+
ref: RefObject<HTMLElement>;
|
|
29
|
+
closePortal: (e?: SyntheticEvent<HTMLElement, Event>) => void;
|
|
30
|
+
togglePortal: (e?: SyntheticEvent<HTMLElement, Event>) => void;
|
|
31
|
+
Portal: ({ children }: {
|
|
32
|
+
children: ReactNode;
|
|
33
|
+
}) => import("react").JSX.Element;
|
|
34
|
+
portalRef: RefObject<HTMLElement>;
|
|
35
|
+
bind: {
|
|
36
|
+
ref: RefObject<HTMLElement>;
|
|
37
|
+
};
|
|
38
|
+
};
|
|
39
|
+
export {};
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.usePortal = exports.errorMessage1 = void 0;
|
|
7
|
+
var _react = require("react");
|
|
8
|
+
var _reactDom = require("react-dom");
|
|
9
|
+
var _useSSR = require("./useSSR");
|
|
10
|
+
/**
|
|
11
|
+
* This is a reference implementation of the usePortal hook from react-useportal: https://github.com/iamthesiz/react-useportal/blob/master/usePortal.ts
|
|
12
|
+
* The license for the content in this file is goverened by the original project's license: https://github.com/iamthesiz/react-useportal/blob/master/license.md
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const errorMessage1 = exports.errorMessage1 = "You must either add a `ref` to the element you are interacting with or pass an `event` to openPortal(e) or togglePortal(e) when the `programmaticallyOpen` option is not set to `true`.";
|
|
16
|
+
const usePortal = function () {
|
|
17
|
+
let {
|
|
18
|
+
closeOnOutsideClick = true,
|
|
19
|
+
closeOnEsc = true,
|
|
20
|
+
bindTo,
|
|
21
|
+
// attach the portal to this node in the DOM
|
|
22
|
+
isOpen: defaultIsOpen = false,
|
|
23
|
+
onOpen,
|
|
24
|
+
onClose,
|
|
25
|
+
onPortalClick,
|
|
26
|
+
programmaticallyOpen = false
|
|
27
|
+
} = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
28
|
+
const {
|
|
29
|
+
isServer,
|
|
30
|
+
isBrowser
|
|
31
|
+
} = (0, _useSSR.useSSR)();
|
|
32
|
+
const [isOpen, makeOpen] = (0, _react.useState)(defaultIsOpen);
|
|
33
|
+
// we use this ref because `isOpen` is stale for handleOutsideMouseClick
|
|
34
|
+
const open = (0, _react.useRef)(isOpen);
|
|
35
|
+
const setOpen = (0, _react.useCallback)(v => {
|
|
36
|
+
// workaround to not have stale `isOpen` in the handleOutsideMouseClick
|
|
37
|
+
open.current = v;
|
|
38
|
+
makeOpen(v);
|
|
39
|
+
}, []);
|
|
40
|
+
const targetEl = (0, _react.useRef)(null); // this is the element you are clicking/hovering/whatever, to trigger opening the portal
|
|
41
|
+
const portal = (0, _react.useRef)(isBrowser ? document.createElement("div") : null);
|
|
42
|
+
(0, _react.useEffect)(() => {
|
|
43
|
+
if (isBrowser && !portal.current) portal.current = document.createElement("div");
|
|
44
|
+
}, [isBrowser, portal]);
|
|
45
|
+
const elToMountTo = (0, _react.useMemo)(() => {
|
|
46
|
+
if (isServer) return null;
|
|
47
|
+
return bindTo && bindTo.current || document.body;
|
|
48
|
+
}, [isServer, bindTo]);
|
|
49
|
+
const createCustomEvent = e => {
|
|
50
|
+
if (!e) return {
|
|
51
|
+
portal,
|
|
52
|
+
targetEl,
|
|
53
|
+
event: e
|
|
54
|
+
};
|
|
55
|
+
const event = e || {};
|
|
56
|
+
if (event.persist) event.persist();
|
|
57
|
+
event.portal = portal;
|
|
58
|
+
event.targetEl = targetEl;
|
|
59
|
+
event.event = e;
|
|
60
|
+
const {
|
|
61
|
+
currentTarget
|
|
62
|
+
} = e;
|
|
63
|
+
if (!targetEl.current && currentTarget && currentTarget !== document) targetEl.current = event.currentTarget;
|
|
64
|
+
return event;
|
|
65
|
+
};
|
|
66
|
+
const openPortal = (0, _react.useCallback)(e => {
|
|
67
|
+
if (isServer) return;
|
|
68
|
+
const customEvent = createCustomEvent(e);
|
|
69
|
+
// for some reason, when we don't have the event argument, there
|
|
70
|
+
// is a weird race condition. Would like to see if we can remove
|
|
71
|
+
// setTimeout, but for now this works
|
|
72
|
+
if (targetEl.current == null && !programmaticallyOpen) {
|
|
73
|
+
setTimeout(() => setOpen(true), 0);
|
|
74
|
+
throw Error(errorMessage1);
|
|
75
|
+
}
|
|
76
|
+
if (onOpen) onOpen(customEvent);
|
|
77
|
+
setOpen(true);
|
|
78
|
+
}, [isServer, portal, setOpen, targetEl, onOpen, programmaticallyOpen]);
|
|
79
|
+
const closePortal = (0, _react.useCallback)(e => {
|
|
80
|
+
if (isServer) return;
|
|
81
|
+
const customEvent = createCustomEvent(e);
|
|
82
|
+
if (onClose && open.current) onClose(customEvent);
|
|
83
|
+
if (open.current) setOpen(false);
|
|
84
|
+
}, [isServer, onClose, setOpen]);
|
|
85
|
+
const togglePortal = (0, _react.useCallback)(e => open.current ? closePortal(e) : openPortal(e), [closePortal, openPortal]);
|
|
86
|
+
const handleKeydown = (0, _react.useCallback)(e => e.key === "Escape" && closeOnEsc ? closePortal(e) : undefined, [closeOnEsc, closePortal]);
|
|
87
|
+
const handleOutsideMouseClick = (0, _react.useCallback)(e => {
|
|
88
|
+
const containsTarget = target => target.current.contains(e.target);
|
|
89
|
+
// There might not be a targetEl if the portal was opened programmatically.
|
|
90
|
+
if (containsTarget(portal) || e.button !== 0 || !open.current || targetEl.current && containsTarget(targetEl)) return;
|
|
91
|
+
if (closeOnOutsideClick) closePortal(e);
|
|
92
|
+
}, [isServer, closePortal, closeOnOutsideClick, portal]);
|
|
93
|
+
const handleMouseDown = (0, _react.useCallback)(e => {
|
|
94
|
+
if (isServer || !(portal.current instanceof HTMLElement)) return;
|
|
95
|
+
const customEvent = createCustomEvent(e);
|
|
96
|
+
if (portal.current.contains(customEvent.target) && onPortalClick) onPortalClick(customEvent);
|
|
97
|
+
handleOutsideMouseClick(e);
|
|
98
|
+
}, [handleOutsideMouseClick, isServer]);
|
|
99
|
+
(0, _react.useEffect)(() => {
|
|
100
|
+
if (isServer) return null;
|
|
101
|
+
if (!(elToMountTo instanceof HTMLElement) || !(portal.current instanceof HTMLElement)) return null;
|
|
102
|
+
const node = portal.current;
|
|
103
|
+
elToMountTo.appendChild(portal.current);
|
|
104
|
+
document.addEventListener("keydown", handleKeydown);
|
|
105
|
+
document.addEventListener("mousedown", handleMouseDown);
|
|
106
|
+
return () => {
|
|
107
|
+
document.removeEventListener("keydown", handleKeydown);
|
|
108
|
+
document.removeEventListener("mousedown", handleMouseDown);
|
|
109
|
+
elToMountTo.removeChild(node);
|
|
110
|
+
};
|
|
111
|
+
}, [isServer, handleOutsideMouseClick, handleKeydown, elToMountTo, portal]);
|
|
112
|
+
const Portal = (0, _react.useCallback)(_ref => {
|
|
113
|
+
let {
|
|
114
|
+
children
|
|
115
|
+
} = _ref;
|
|
116
|
+
if (portal.current != null) return /*#__PURE__*/(0, _reactDom.createPortal)(children, portal.current);
|
|
117
|
+
return null;
|
|
118
|
+
}, [portal]);
|
|
119
|
+
return Object.assign([openPortal, closePortal, open.current, Portal, togglePortal, targetEl, portal], {
|
|
120
|
+
isOpen: open.current,
|
|
121
|
+
openPortal,
|
|
122
|
+
ref: targetEl,
|
|
123
|
+
closePortal,
|
|
124
|
+
togglePortal,
|
|
125
|
+
Portal,
|
|
126
|
+
portalRef: portal,
|
|
127
|
+
bind: {
|
|
128
|
+
// used if you want to spread all html attributes onto the target element
|
|
129
|
+
ref: targetEl
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
exports.usePortal = usePortal;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is a reference implementation of the usePortal hook from react-useportal: https://github.com/iamthesiz/react-useportal/blob/master/usePortal.test.ts
|
|
3
|
+
* The license for the content in this file is goverened by the original project's license: https://github.com/iamthesiz/react-useportal/blob/master/license.md
|
|
4
|
+
*/
|
|
5
|
+
export {};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This is a reference implementation of the useSSR hook from use-ssr: https://github.com/iamthesiz/use-ssr/blob/master/useSSR.ts
|
|
3
|
+
* The license for the content in this file is goverened by the original project's license: https://github.com/iamthesiz/use-ssr/blob/master/license.md
|
|
4
|
+
*/
|
|
5
|
+
interface UseSSRReturn {
|
|
6
|
+
isBrowser: boolean;
|
|
7
|
+
isServer: boolean;
|
|
8
|
+
device: Device;
|
|
9
|
+
canUseWorkers: boolean;
|
|
10
|
+
canUseEventListeners: boolean;
|
|
11
|
+
canUseViewport: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare enum Device {
|
|
14
|
+
Browser = "browser",
|
|
15
|
+
Server = "server"
|
|
16
|
+
}
|
|
17
|
+
export declare const weAreServer: () => void;
|
|
18
|
+
export declare const useSSR: () => UseSSRReturn;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.weAreServer = exports.useSSR = exports.Device = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* This is a reference implementation of the useSSR hook from use-ssr: https://github.com/iamthesiz/use-ssr/blob/master/useSSR.ts
|
|
9
|
+
* The license for the content in this file is goverened by the original project's license: https://github.com/iamthesiz/use-ssr/blob/master/license.md
|
|
10
|
+
*/
|
|
11
|
+
let Device = exports.Device = /*#__PURE__*/function (Device) {
|
|
12
|
+
Device["Browser"] = "browser";
|
|
13
|
+
Device["Server"] = "server";
|
|
14
|
+
return Device;
|
|
15
|
+
}({});
|
|
16
|
+
const {
|
|
17
|
+
Browser,
|
|
18
|
+
Server
|
|
19
|
+
} = Device;
|
|
20
|
+
const canUseDOM = !!(typeof window !== "undefined" && window.document && window.document.createElement);
|
|
21
|
+
const device = canUseDOM ? Browser : Server;
|
|
22
|
+
const SSRObject = {
|
|
23
|
+
isBrowser: device === Browser,
|
|
24
|
+
isServer: device === Server,
|
|
25
|
+
device,
|
|
26
|
+
canUseWorkers: typeof Worker !== "undefined",
|
|
27
|
+
canUseEventListeners: device === Browser && !!window.addEventListener,
|
|
28
|
+
canUseViewport: device === Browser && !!window.screen
|
|
29
|
+
};
|
|
30
|
+
const assign = function () {
|
|
31
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
32
|
+
args[_key] = arguments[_key];
|
|
33
|
+
}
|
|
34
|
+
return args.reduce((acc, obj) => ({
|
|
35
|
+
...acc,
|
|
36
|
+
...obj
|
|
37
|
+
}), {});
|
|
38
|
+
};
|
|
39
|
+
const values = obj => Object.keys(obj).map(key => obj[key]);
|
|
40
|
+
const toArrayObject = () => assign((values(SSRObject), SSRObject));
|
|
41
|
+
let useSSRObject = toArrayObject();
|
|
42
|
+
const weAreServer = () => {
|
|
43
|
+
SSRObject.isServer = true;
|
|
44
|
+
useSSRObject = toArrayObject();
|
|
45
|
+
};
|
|
46
|
+
exports.weAreServer = weAreServer;
|
|
47
|
+
const useSSR = () => useSSRObject;
|
|
48
|
+
exports.useSSR = useSSR;
|
|
@@ -16,8 +16,8 @@ const THROTTLE_DELAY = exports.THROTTLE_DELAY = 1000 / 60;
|
|
|
16
16
|
*/
|
|
17
17
|
const useThrottle = function (callback) {
|
|
18
18
|
let delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : THROTTLE_DELAY;
|
|
19
|
-
const timeout = (0, _react.useRef)();
|
|
20
|
-
const lastCall = (0, _react.useRef)();
|
|
19
|
+
const timeout = (0, _react.useRef)(null);
|
|
20
|
+
const lastCall = (0, _react.useRef)(null);
|
|
21
21
|
const lastArgs = (0, _react.useRef)(null);
|
|
22
22
|
const throttle = (0, _react.useCallback)(function () {
|
|
23
23
|
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
package/dist/index.d.ts
CHANGED
|
@@ -138,3 +138,5 @@ export type { WindowFitment } from "./hooks";
|
|
|
138
138
|
export { isNavigationAnchor, isNavigationButton } from "./utils";
|
|
139
139
|
export type { ClassName, Headings, PropsWithSpread, SortDirection, SubComponentProps, TSFixMe, ValueOf, } from "./types";
|
|
140
140
|
export { Theme } from "./enums";
|
|
141
|
+
export type { UsePortalOptions } from "./external";
|
|
142
|
+
export { usePortal } from "./external";
|
package/dist/index.js
CHANGED
|
@@ -96,7 +96,8 @@ var _exportNames = {
|
|
|
96
96
|
useWindowFitment: true,
|
|
97
97
|
isNavigationAnchor: true,
|
|
98
98
|
isNavigationButton: true,
|
|
99
|
-
Theme: true
|
|
99
|
+
Theme: true,
|
|
100
|
+
usePortal: true
|
|
100
101
|
};
|
|
101
102
|
Object.defineProperty(exports, "Accordion", {
|
|
102
103
|
enumerable: true,
|
|
@@ -638,6 +639,12 @@ Object.defineProperty(exports, "usePagination", {
|
|
|
638
639
|
return _hooks.usePagination;
|
|
639
640
|
}
|
|
640
641
|
});
|
|
642
|
+
Object.defineProperty(exports, "usePortal", {
|
|
643
|
+
enumerable: true,
|
|
644
|
+
get: function () {
|
|
645
|
+
return _external.usePortal;
|
|
646
|
+
}
|
|
647
|
+
});
|
|
641
648
|
Object.defineProperty(exports, "usePrevious", {
|
|
642
649
|
enumerable: true,
|
|
643
650
|
get: function () {
|
|
@@ -740,6 +747,7 @@ var _CustomSelect = _interopRequireDefault(require("./components/CustomSelect"))
|
|
|
740
747
|
var _hooks = require("./hooks");
|
|
741
748
|
var _utils = require("./utils");
|
|
742
749
|
var _enums = require("./enums");
|
|
750
|
+
var _external = require("./external");
|
|
743
751
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
744
752
|
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
745
753
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|