draft-components 1.12.0 → 1.13.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.
@@ -19,7 +19,7 @@ const backdropFade = [
19
19
  { opacity: 0 },
20
20
  { opacity: 1 },
21
21
  ];
22
- function SlideOver({ 'aria-labelledby': ariaLabelledby, 'aria-describedby': ariaDescribedby, animationDurationMs = 350, animationDisabled = false, className, children, onClose, ...props }) {
22
+ const SlideOverWithRef = react.forwardRef(function SlideOver({ 'aria-labelledby': ariaLabelledby, 'aria-describedby': ariaDescribedby, animationDurationMs = 350, animationDisabled = false, shouldCloseOnEscKeyPress = true, shouldCloseOnBackdropClick = true, className, children, onClose, ...props }, ref) {
23
23
  const id = react.useId();
24
24
  const titleId = ariaLabelledby || `${id}dialogTitle`;
25
25
  const descriptionId = ariaDescribedby || `${id}dialogDescription`;
@@ -73,6 +73,9 @@ function SlideOver({ 'aria-labelledby': ariaLabelledby, 'aria-describedby': aria
73
73
  });
74
74
  };
75
75
  }, [animationDurationMs, onClose, shouldPlayAnimations]);
76
+ react.useImperativeHandle(ref, () => ({
77
+ close: closePanel,
78
+ }));
76
79
  react.useEffect(() => {
77
80
  isAnimationDisabled.current = animationDisabled;
78
81
  }, [animationDisabled]);
@@ -92,15 +95,19 @@ function SlideOver({ 'aria-labelledby': ariaLabelledby, 'aria-describedby': aria
92
95
  });
93
96
  }
94
97
  }, [shouldPlayAnimations, animationDurationMs]);
95
- useEscKeyDown.useEscKeyDown(() => closePanel('escape'));
98
+ useEscKeyDown.useEscKeyDown(() => closePanel('escape'), { isEnabled: shouldCloseOnEscKeyPress });
96
99
  useFocusTrap.useFocusTrap(panelRef);
97
100
  useDisableBodyScroll.useDisableBodyScroll();
98
101
  const onClickBackdrop = () => {
99
- closePanel('backdrop');
102
+ if (shouldCloseOnBackdropClick) {
103
+ closePanel('backdrop');
104
+ }
100
105
  };
101
106
  return (jsxRuntime.jsx(portal.Portal, { children: jsxRuntime.jsxs("div", { className: "dc-slide-over", children: [jsxRuntime.jsx("div", { "data-testid": "slide-over-backdrop", className: "dc-slide-over__backdrop", ref: backdropRef, onClick: onClickBackdrop }), jsxRuntime.jsx("div", { ...props, className: reactHelpers.classNames('dc-slide-over__panel', className), role: "dialog", ref: panelRef, "aria-modal": true, "aria-labelledby": titleId, "aria-describedby": descriptionId, children: jsxRuntime.jsx(slideOverContext.SlideOverContextProvider, { titleId: titleId, descriptionId: descriptionId, closePanel: closePanel, children: children }) })] }) }));
102
- }
103
- SlideOver.Header = slideOverHeader.SlideOverHeader;
104
- SlideOver.Body = slideOverBody.SlideOverBody;
107
+ });
108
+ const SlideOver = Object.assign(SlideOverWithRef, {
109
+ Header: slideOverHeader.SlideOverHeader,
110
+ Body: slideOverBody.SlideOverBody,
111
+ });
105
112
 
106
113
  exports.SlideOver = SlideOver;
@@ -1,5 +1,5 @@
1
1
  import { jsx } from 'react/jsx-runtime';
2
- import { createContext, useContext, useMemo } from 'react';
2
+ import { createContext, useMemo, useContext } from 'react';
3
3
 
4
4
  const Context = createContext(null);
5
5
  function useSlideOverContext() {
@@ -1,5 +1,5 @@
1
1
  import { jsx, jsxs } from 'react/jsx-runtime';
2
- import { useId, useRef, useCallback, useMemo, useEffect } from 'react';
2
+ import { forwardRef, useId, useRef, useCallback, useMemo, useImperativeHandle, useEffect } from 'react';
3
3
  import { SlideOverContextProvider } from './slide-over-context.js';
4
4
  import { getRefElement, classNames } from '../../lib/react-helpers.js';
5
5
  import { useDisableBodyScroll } from '../../hooks/use-disable-body-scroll.js';
@@ -17,7 +17,7 @@ const backdropFade = [
17
17
  { opacity: 0 },
18
18
  { opacity: 1 },
19
19
  ];
20
- function SlideOver({ 'aria-labelledby': ariaLabelledby, 'aria-describedby': ariaDescribedby, animationDurationMs = 350, animationDisabled = false, className, children, onClose, ...props }) {
20
+ const SlideOverWithRef = forwardRef(function SlideOver({ 'aria-labelledby': ariaLabelledby, 'aria-describedby': ariaDescribedby, animationDurationMs = 350, animationDisabled = false, shouldCloseOnEscKeyPress = true, shouldCloseOnBackdropClick = true, className, children, onClose, ...props }, ref) {
21
21
  const id = useId();
22
22
  const titleId = ariaLabelledby || `${id}dialogTitle`;
23
23
  const descriptionId = ariaDescribedby || `${id}dialogDescription`;
@@ -71,6 +71,9 @@ function SlideOver({ 'aria-labelledby': ariaLabelledby, 'aria-describedby': aria
71
71
  });
72
72
  };
73
73
  }, [animationDurationMs, onClose, shouldPlayAnimations]);
74
+ useImperativeHandle(ref, () => ({
75
+ close: closePanel,
76
+ }));
74
77
  useEffect(() => {
75
78
  isAnimationDisabled.current = animationDisabled;
76
79
  }, [animationDisabled]);
@@ -90,15 +93,19 @@ function SlideOver({ 'aria-labelledby': ariaLabelledby, 'aria-describedby': aria
90
93
  });
91
94
  }
92
95
  }, [shouldPlayAnimations, animationDurationMs]);
93
- useEscKeyDown(() => closePanel('escape'));
96
+ useEscKeyDown(() => closePanel('escape'), { isEnabled: shouldCloseOnEscKeyPress });
94
97
  useFocusTrap(panelRef);
95
98
  useDisableBodyScroll();
96
99
  const onClickBackdrop = () => {
97
- closePanel('backdrop');
100
+ if (shouldCloseOnBackdropClick) {
101
+ closePanel('backdrop');
102
+ }
98
103
  };
99
104
  return (jsx(Portal, { children: jsxs("div", { className: "dc-slide-over", children: [jsx("div", { "data-testid": "slide-over-backdrop", className: "dc-slide-over__backdrop", ref: backdropRef, onClick: onClickBackdrop }), jsx("div", { ...props, className: classNames('dc-slide-over__panel', className), role: "dialog", ref: panelRef, "aria-modal": true, "aria-labelledby": titleId, "aria-describedby": descriptionId, children: jsx(SlideOverContextProvider, { titleId: titleId, descriptionId: descriptionId, closePanel: closePanel, children: children }) })] }) }));
100
- }
101
- SlideOver.Header = SlideOverHeader;
102
- SlideOver.Body = SlideOverBody;
105
+ });
106
+ const SlideOver = Object.assign(SlideOverWithRef, {
107
+ Header: SlideOverHeader,
108
+ Body: SlideOverBody,
109
+ });
103
110
 
104
111
  export { SlideOver };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "draft-components",
3
- "version": "1.12.0",
3
+ "version": "1.13.0",
4
4
  "description": "The React based UI components library.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -1,4 +1,4 @@
1
1
  export { type SlideOverCloseCallback } from './types';
2
- export { SlideOver, type SlideOverProps } from './slide-over';
2
+ export { SlideOver, type SlideOverProps, type SlideOverRef, } from './slide-over';
3
3
  export { SlideOverHeader, type SlideOverHeaderProps, } from './slide-over-header';
4
4
  export { SlideOverBody, type SlideOverBodyProps, } from './slide-over-body';
@@ -6,10 +6,20 @@ export type SlideOverHTMLProps = ComponentPropsWithoutRef<'div'>;
6
6
  export type SlideOverProps = SlideOverHTMLProps & {
7
7
  animationDurationMs?: number;
8
8
  animationDisabled?: boolean;
9
+ shouldCloseOnEscKeyPress?: boolean;
10
+ shouldCloseOnBackdropClick?: boolean;
9
11
  onClose: SlideOverCloseCallback;
10
12
  };
11
- export declare function SlideOver({ 'aria-labelledby': ariaLabelledby, 'aria-describedby': ariaDescribedby, animationDurationMs, animationDisabled, className, children, onClose, ...props }: SlideOverProps): import("react/jsx-runtime").JSX.Element;
12
- export declare namespace SlideOver {
13
- var Header: typeof SlideOverHeader;
14
- var Body: typeof SlideOverBody;
15
- }
13
+ export type SlideOverRef = {
14
+ close: () => void;
15
+ };
16
+ export declare const SlideOver: import("react").ForwardRefExoticComponent<Omit<import("react").DetailedHTMLProps<import("react").HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & {
17
+ animationDurationMs?: number | undefined;
18
+ animationDisabled?: boolean | undefined;
19
+ shouldCloseOnEscKeyPress?: boolean | undefined;
20
+ shouldCloseOnBackdropClick?: boolean | undefined;
21
+ onClose: SlideOverCloseCallback;
22
+ } & import("react").RefAttributes<SlideOverRef>> & {
23
+ Header: typeof SlideOverHeader;
24
+ Body: typeof SlideOverBody;
25
+ };
@@ -1 +1 @@
1
- export type SlideOverCloseCallback = (source: 'close-button' | 'backdrop' | 'escape') => void;
1
+ export type SlideOverCloseCallback = (source?: 'close-button' | 'backdrop' | 'escape') => void;