@kvdbil/components 17.1.0 → 17.2.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.
@@ -1,4 +1,5 @@
1
1
  "use strict";var e,t=require("react/jsx-runtime"),r=require("react"),n=require("styled-components");const u=((e=n)&&e.__esModule?e:{default:e}).default.div`
2
2
  display: flex;
3
+ width: 100%;
3
4
  height: 100%;
4
- `;exports.FocusTrap=({children:e,pause:n=!1})=>{const c=r.useRef(null),s=r.useRef([]),o=r.useRef(null),i=r.useRef(null),l=r.useRef(!1);return r.useEffect((()=>{if(n)return void(l.current&&i.current&&i.current.focus());const e=c.current;if(!e)return;i.current=document.activeElement;const t=["a[href]","button:not([disabled])","textarea:not([disabled])","input:not([disabled])","select:not([disabled])",'[tabindex]:not([tabindex="-1"])'],r=()=>{s.current=Array.from(e.querySelectorAll(t.join(",")))},u=e=>{if("Tab"!==e.key)return;const t=s.current;if(0===t.length)return void e.preventDefault();const r=t[0],n=t[t.length-1],u=t.findIndex((e=>e===document.activeElement));e.preventDefault(),e.shiftKey?u<=0?n.focus():t[u-1].focus():-1===u||u===t.length-1?r.focus():t[u+1].focus()};return r(),s.current.length>0&&s.current[0].focus(),e.addEventListener("keydown",u),o.current=new MutationObserver(r),o.current.observe(e,{childList:!0,subtree:!0}),l.current=!0,()=>{e.removeEventListener("keydown",u),o.current?.disconnect(),o.current=null}}),[n]),t.jsx(u,{ref:c,children:e})};
5
+ `;exports.FocusTrap=({children:e,pause:n=!1,autoFocusOnOpen:s="firstElem",restoreFocusOnClose:c=!0})=>{const o=r.useRef(null),i=r.useRef([]),l=r.useRef(null),d=r.useRef(null),f=r.useRef(!1);return r.useEffect((()=>{if(n)return void(c&&f.current&&d.current&&d.current.focus());const e=o.current;if(!e)return;c&&(d.current=document.activeElement);const t=["a[href]","button:not([disabled])","textarea:not([disabled])",'input:not([disabled]):not([type="hidden"])',"select:not([disabled])",'[tabindex]:not([tabindex="-1"])'],r=()=>{i.current=Array.from(e.querySelectorAll(t.join(",")))},u=e=>{if("Tab"!==e.key)return;const t=i.current;if(0===t.length)return void e.preventDefault();const r=t[0],n=t[t.length-1],u=t.findIndex((e=>e===document.activeElement));e.preventDefault(),e.shiftKey?u<=0?n.focus():t[u-1].focus():-1===u||u===t.length-1?r.focus():t[u+1].focus()};return r(),"firstElem"===s&&i.current.length>0&&i.current[0].focus(),"firstInput"===s&&i.current.find((e=>"INPUT"===e.tagName))?.focus(),e.addEventListener("keydown",u),l.current=new MutationObserver(r),l.current.observe(e,{childList:!0,subtree:!0}),f.current=!0,()=>{e.removeEventListener("keydown",u),l.current?.disconnect(),l.current=null}}),[n,s,c]),t.jsx(u,{ref:o,children:e})};
@@ -77,4 +77,4 @@
77
77
  }
78
78
  `,b=r.createGlobalStyle`
79
79
  ${u}
80
- `,h=({children:r,isOpen:a,contentStyles:d,withPortal:l=!1,withCloseIcon:u=!0,onClose:h,...w})=>{t.useEffect((()=>{const e=e=>{"Escape"===e.key&&a&&h()};return a&&window.addEventListener("keydown",e),()=>{window.removeEventListener("keydown",e)}}),[h,a]);const f=!!(typeof window<"u"&&window.document&&window.document.createElement),y=e.jsx(o.CSSTransition,{in:a,mountOnEnter:!0,unmountOnExit:!0,timeout:400,classNames:"backgroundTransition",children:e.jsxs(e.Fragment,{children:[e.jsx(b,{}),e.jsxs(c,{...w,children:[e.jsx(p,{"data-testid":"background",onClick:h}),e.jsx(s.FocusTrap,{pause:!a,children:e.jsxs(x,{style:d,role:"dialog","aria-modal":"true","aria-labelledby":"modal-title",children:[u&&e.jsx(m,{onClick:h,"data-testid":"close",type:"button","aria-label":"Close dialog",children:e.jsx(n.default,{})}),r]})}),e.jsx(g,{})]})]})});return l&&f?i.createPortal(y,document.body):y};exports.Modal=h,exports.default=h;
80
+ `,h=({children:r,isOpen:a,contentStyles:d,withPortal:l=!1,withCloseIcon:u=!0,onClose:h,focusTrapProps:w={},...f})=>{t.useEffect((()=>{const e=e=>{"Escape"===e.key&&a&&h()};return a&&window.addEventListener("keydown",e),()=>{window.removeEventListener("keydown",e)}}),[h,a]);const y=!!(typeof window<"u"&&window.document&&window.document.createElement),v=e.jsx(o.CSSTransition,{in:a,mountOnEnter:!0,unmountOnExit:!0,timeout:400,classNames:"backgroundTransition",children:e.jsxs(e.Fragment,{children:[e.jsx(b,{}),e.jsxs(c,{...f,children:[e.jsx(p,{"data-testid":"background",onClick:h}),e.jsx(s.FocusTrap,{pause:!a,...w,children:e.jsxs(x,{style:d,role:"dialog","aria-modal":"true","aria-labelledby":"modal-title",children:[u&&e.jsx(m,{onClick:h,"data-testid":"close",type:"button","aria-label":"Close dialog",children:e.jsx(n.default,{})}),r]})}),e.jsx(g,{})]})]})});return l&&y?i.createPortal(v,document.body):v};exports.Modal=h,exports.default=h;
@@ -7,4 +7,4 @@
7
7
  overflow-x: hidden;
8
8
  overflow-y: auto;
9
9
  -webkit-overflow-scrolling: touch;
10
- `,m=({animationName:r="offCanvasAnimation",animationDuration:l=300,backgroundColor:d,backgroundOverlay:m,children:h,from:$="right",isOpen:g,onClickOutside:x,width:v="375px",withPortal:p,transitionProps:w})=>{const k=e.useRef(null);o.useOutsideClick(k,(()=>x?.())),e.useEffect((()=>{const n=n=>{"Escape"===n.key&&g&&x?.()};return g&&window.addEventListener("keydown",n),()=>{window.removeEventListener("keydown",n)}}),[x,g]),m&&!x&&console.error("Please provide a clickOutside handler when using backgroundOverlay option in OffCanvas component");const O=`${r}--${$}`,b=n.jsxs(n.Fragment,{children:[m&&n.jsx(i.default,{isOpen:g}),n.jsx(n.Fragment,{children:n.jsx(a.CSSTransition,{in:g,className:`${r} ${O}`,classNames:O,...u,...w,children:n.jsx(s.FocusTrap,{pause:!g,children:n.jsx(c,{ref:k,animationName:r,animationDuration:l,backgroundColor:d,from:$,width:v,role:"dialog","aria-modal":"true","aria-label":"Side panel",children:n.jsx(f,{className:`${r}-content`,children:h})})})})})]});return p?t.createPortal(b,document.body):b};exports.OffCanvas=m,exports.default=m;
10
+ `,m=({animationName:r="offCanvasAnimation",animationDuration:l=300,backgroundColor:d,backgroundOverlay:m,children:h,from:$="right",isOpen:g,onClickOutside:x,width:p="375px",withPortal:v,transitionProps:w,focusTrapProps:k={}})=>{const O=e.useRef(null);o.useOutsideClick(O,(()=>x?.())),e.useEffect((()=>{const n=n=>{"Escape"===n.key&&g&&x?.()};return g&&window.addEventListener("keydown",n),()=>{window.removeEventListener("keydown",n)}}),[x,g]),m&&!x&&console.error("Please provide a clickOutside handler when using backgroundOverlay option in OffCanvas component");const b=`${r}--${$}`,j=n.jsxs(n.Fragment,{children:[m&&n.jsx(i.default,{isOpen:g}),n.jsx(n.Fragment,{children:n.jsx(a.CSSTransition,{in:g,className:`${r} ${b}`,classNames:b,...u,...w,children:n.jsx(s.FocusTrap,{pause:!g,...k,children:n.jsx(c,{ref:O,animationName:r,animationDuration:l,backgroundColor:d,from:$,width:p,role:"dialog","aria-modal":"true","aria-label":"Side panel",children:n.jsx(f,{className:`${r}-content`,children:h})})})})})]});return v?t.createPortal(j,document.body):j};exports.OffCanvas=m,exports.default=m;
@@ -1,4 +1,5 @@
1
- import{jsx as x}from"react/jsx-runtime";import{useRef as n,useEffect as y}from"react";import g from"styled-components";const E=g.div`
1
+ import{jsx as x}from"react/jsx-runtime";import{useRef as o,useEffect as g}from"react";import w from"styled-components";const L=w.div`
2
2
  display: flex;
3
+ width: 100%;
3
4
  height: 100%;
4
- `,k=({children:m,pause:l=!1})=>{const i=n(null),u=n([]),o=n(null),s=n(null),f=n(!1);return y(()=>{if(l){f.current&&s.current&&s.current.focus();return}const t=i.current;if(!t)return;s.current=document.activeElement;const b=["a[href]","button:not([disabled])","textarea:not([disabled])","input:not([disabled])","select:not([disabled])",'[tabindex]:not([tabindex="-1"])'],d=()=>{u.current=Array.from(t.querySelectorAll(b.join(",")))},a=c=>{if(c.key!=="Tab")return;const e=u.current;if(e.length===0){c.preventDefault();return}const h=e[0],p=e[e.length-1],r=e.findIndex(v=>v===document.activeElement);c.preventDefault(),c.shiftKey?r<=0?p.focus():e[r-1].focus():r===-1||r===e.length-1?h.focus():e[r+1].focus()};return d(),u.current.length>0&&u.current[0].focus(),t.addEventListener("keydown",a),o.current=new MutationObserver(d),o.current.observe(t,{childList:!0,subtree:!0}),f.current=!0,()=>{t.removeEventListener("keydown",a),o.current?.disconnect(),o.current=null}},[l]),x(E,{ref:i,children:m})};export{k as FocusTrap};
5
+ `,O=({children:p,pause:f=!1,autoFocusOnOpen:s="firstElem",restoreFocusOnClose:i=!0})=>{const d=o(null),r=o([]),c=o(null),l=o(null),a=o(!1);return g(()=>{if(f){i&&a.current&&l.current&&l.current.focus();return}const n=d.current;if(!n)return;i&&(l.current=document.activeElement);const b=["a[href]","button:not([disabled])","textarea:not([disabled])",'input:not([disabled]):not([type="hidden"])',"select:not([disabled])",'[tabindex]:not([tabindex="-1"])'],m=()=>{r.current=Array.from(n.querySelectorAll(b.join(",")))},h=t=>{if(t.key!=="Tab")return;const e=r.current;if(e.length===0){t.preventDefault();return}const v=e[0],y=e[e.length-1],u=e.findIndex(E=>E===document.activeElement);t.preventDefault(),t.shiftKey?u<=0?y.focus():e[u-1].focus():u===-1||u===e.length-1?v.focus():e[u+1].focus()};return m(),s==="firstElem"&&r.current.length>0&&r.current[0].focus(),s==="firstInput"&&r.current.find(t=>t.tagName==="INPUT")?.focus(),n.addEventListener("keydown",h),c.current=new MutationObserver(m),c.current.observe(n,{childList:!0,subtree:!0}),a.current=!0,()=>{n.removeEventListener("keydown",h),c.current?.disconnect(),c.current=null}},[f,s,i]),x(L,{ref:d,children:p})};export{O as FocusTrap};
@@ -1,4 +1,4 @@
1
- import{jsx as t,jsxs as n,Fragment as f}from"react/jsx-runtime";import{useEffect as w}from"react";import{createPortal as b}from"react-dom";import{CSSTransition as y}from"react-transition-group";import e,{css as v,createGlobalStyle as d}from"styled-components";import k from"../../icons/components/CloseIcon.js";import{resetButtonStyle as T}from"../../utils/utils.js";import{FocusTrap as E}from"../FocusTrap.js";const s=v`
1
+ import{jsx as t,jsxs as r,Fragment as w}from"react/jsx-runtime";import{useEffect as b}from"react";import{createPortal as y}from"react-dom";import{CSSTransition as v}from"react-transition-group";import e,{css as k,createGlobalStyle as d}from"styled-components";import T from"../../icons/components/CloseIcon.js";import{resetButtonStyle as E}from"../../utils/utils.js";import{FocusTrap as C}from"../FocusTrap.js";const s=k`
2
2
  .backgroundTransition-enter {
3
3
  opacity: 0;
4
4
  }
@@ -16,7 +16,7 @@ import{jsx as t,jsxs as n,Fragment as f}from"react/jsx-runtime";import{useEffect
16
16
  opacity: 0;
17
17
  transition: opacity 0.25s ease-in-out;
18
18
  }
19
- `,C=e.div`
19
+ `,S=e.div`
20
20
  ${s}
21
21
  position: fixed;
22
22
  width: 100%;
@@ -24,7 +24,7 @@ import{jsx as t,jsxs as n,Fragment as f}from"react/jsx-runtime";import{useEffect
24
24
  top: 0;
25
25
  left: 0;
26
26
  z-index: 200;
27
- `,S=e.div`
27
+ `,P=e.div`
28
28
  z-index: -1;
29
29
  position: absolute;
30
30
  width: 100%;
@@ -54,7 +54,7 @@ import{jsx as t,jsxs as n,Fragment as f}from"react/jsx-runtime";import{useEffect
54
54
  min-width: 355px;
55
55
  }
56
56
  `,j=e.button`
57
- ${T}
57
+ ${E}
58
58
  display: flex;
59
59
  justify-self: flex-end;
60
60
 
@@ -77,6 +77,6 @@ import{jsx as t,jsxs as n,Fragment as f}from"react/jsx-runtime";import{useEffect
77
77
  padding-right: 15px !important;
78
78
  `}
79
79
  }
80
- `,O=d`
80
+ `,F=d`
81
81
  ${s}
82
- `,l=({children:c,isOpen:i,contentStyles:p,withPortal:m=!1,withCloseIcon:u=!0,onClose:o,...g})=>{w(()=>{const a=x=>{x.key==="Escape"&&i&&o()};return i&&window.addEventListener("keydown",a),()=>{window.removeEventListener("keydown",a)}},[o,i]);const h=!!(typeof window<"u"&&window.document&&window.document.createElement),r=t(y,{in:i,mountOnEnter:!0,unmountOnExit:!0,timeout:400,classNames:"backgroundTransition",children:n(f,{children:[t(O,{}),n(C,{...g,children:[t(S,{"data-testid":"background",onClick:o}),t(E,{pause:!i,children:n($,{style:p,role:"dialog","aria-modal":"true","aria-labelledby":"modal-title",children:[u&&t(j,{onClick:o,"data-testid":"close",type:"button","aria-label":"Close dialog",children:t(k,{})}),c]})}),t(z,{})]})]})});return m&&h?b(r,document.body):r};export{l as Modal,l as default};
82
+ `,l=({children:c,isOpen:o,contentStyles:p,withPortal:m=!1,withCloseIcon:u=!0,onClose:i,focusTrapProps:g={},...f})=>{b(()=>{const a=x=>{x.key==="Escape"&&o&&i()};return o&&window.addEventListener("keydown",a),()=>{window.removeEventListener("keydown",a)}},[i,o]);const h=!!(typeof window<"u"&&window.document&&window.document.createElement),n=t(v,{in:o,mountOnEnter:!0,unmountOnExit:!0,timeout:400,classNames:"backgroundTransition",children:r(w,{children:[t(F,{}),r(S,{...f,children:[t(P,{"data-testid":"background",onClick:i}),t(C,{pause:!o,...g,children:r($,{style:p,role:"dialog","aria-modal":"true","aria-labelledby":"modal-title",children:[u&&t(j,{onClick:i,"data-testid":"close",type:"button","aria-label":"Close dialog",children:t(T,{})}),c]})}),t(z,{})]})]})});return m&&h?y(n,document.body):n};export{l as Modal,l as default};
@@ -1,4 +1,4 @@
1
- import{jsxs as x,Fragment as u,jsx as n}from"react/jsx-runtime";import{useRef as k,useEffect as b}from"react";import{createPortal as O}from"react-dom";import h from"styled-components";import{CSSTransition as C}from"react-transition-group";import y from"./Overlay.js";import{useOutsideClick as E}from"./useClickOutside.js";import{FocusTrap as N}from"../FocusTrap.js";const P="offCanvasAnimation",S={timeout:{enter:300,exit:300},mountOnEnter:!0,unmountOnExit:!0},D=h.div`
1
+ import{jsxs as k,Fragment as u,jsx as n}from"react/jsx-runtime";import{useRef as b,useEffect as O}from"react";import{createPortal as C}from"react-dom";import h from"styled-components";import{CSSTransition as y}from"react-transition-group";import E from"./Overlay.js";import{useOutsideClick as N}from"./useClickOutside.js";import{FocusTrap as P}from"../FocusTrap.js";const D="offCanvasAnimation",S={timeout:{enter:300,exit:300},mountOnEnter:!0,unmountOnExit:!0},T=h.div`
2
2
  ${({animationName:t,animationDuration:s,width:e,from:o,backgroundColor:l,theme:i})=>`
3
3
  position: fixed;
4
4
  z-index: 1001;
@@ -54,4 +54,4 @@ import{jsxs as x,Fragment as u,jsx as n}from"react/jsx-runtime";import{useRef as
54
54
  overflow-x: hidden;
55
55
  overflow-y: auto;
56
56
  -webkit-overflow-scrolling: touch;
57
- `,$=({animationName:t=P,animationDuration:s=300,backgroundColor:e,backgroundOverlay:o,children:l,from:i="right",isOpen:r,onClickOutside:a,width:g="375px",withPortal:p,transitionProps:v})=>{const m=k(null);E(m,()=>a?.()),b(()=>{const c=w=>{w.key==="Escape"&&r&&a?.()};return r&&window.addEventListener("keydown",c),()=>{window.removeEventListener("keydown",c)}},[a,r]),o&&!a&&console.error("Please provide a clickOutside handler when using backgroundOverlay option in OffCanvas component");const d=`${t}--${i}`,f=x(u,{children:[o&&n(y,{isOpen:r}),n(u,{children:n(C,{in:r,className:`${t} ${d}`,classNames:d,...S,...v,children:n(N,{pause:!r,children:n(D,{ref:m,animationName:t,animationDuration:s,backgroundColor:e,from:i,width:g,role:"dialog","aria-modal":"true","aria-label":"Side panel",children:n(j,{className:`${t}-content`,children:l})})})})})]});return p?O(f,document.body):f};export{$ as OffCanvas,$ as default};
57
+ `,$=({animationName:t=D,animationDuration:s=300,backgroundColor:e,backgroundOverlay:o,children:l,from:i="right",isOpen:r,onClickOutside:a,width:p="375px",withPortal:g,transitionProps:v,focusTrapProps:w={}})=>{const m=b(null);N(m,()=>a?.()),O(()=>{const c=x=>{x.key==="Escape"&&r&&a?.()};return r&&window.addEventListener("keydown",c),()=>{window.removeEventListener("keydown",c)}},[a,r]),o&&!a&&console.error("Please provide a clickOutside handler when using backgroundOverlay option in OffCanvas component");const d=`${t}--${i}`,f=k(u,{children:[o&&n(E,{isOpen:r}),n(u,{children:n(y,{in:r,className:`${t} ${d}`,classNames:d,...S,...v,children:n(P,{pause:!r,...w,children:n(T,{ref:m,animationName:t,animationDuration:s,backgroundColor:e,from:i,width:p,role:"dialog","aria-modal":"true","aria-label":"Side panel",children:n(j,{className:`${t}-content`,children:l})})})})})]});return g?C(f,document.body):f};export{$ as OffCanvas,$ as default};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kvdbil/components",
3
- "version": "17.1.0",
3
+ "version": "17.2.0",
4
4
  "sideEffects": false,
5
5
  "main": "cjs/index.js",
6
6
  "module": "esm/index.js",
package/package.json.tmp CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kvdbil/components",
3
- "version": "17.1.0",
3
+ "version": "17.2.0",
4
4
  "sideEffects": false,
5
5
  "main": "cjs/index.js",
6
6
  "module": "esm/index.js",
@@ -1,7 +1,8 @@
1
1
  import React from 'react';
2
- interface FocusTrapProps {
2
+ export interface FocusTrapProps {
3
3
  children: React.ReactNode;
4
4
  pause?: boolean;
5
+ autoFocusOnOpen?: 'firstElem' | 'firstInput' | 'none';
6
+ restoreFocusOnClose?: boolean;
5
7
  }
6
- export declare const FocusTrap: ({ children, pause }: FocusTrapProps) => import("react/jsx-runtime").JSX.Element;
7
- export {};
8
+ export declare const FocusTrap: ({ children, pause, autoFocusOnOpen, restoreFocusOnClose, }: FocusTrapProps) => import("react/jsx-runtime").JSX.Element;
@@ -1,4 +1,5 @@
1
1
  import { ReactNode } from 'react';
2
+ import { FocusTrapProps } from '../FocusTrap';
2
3
  export interface ModalProps {
3
4
  title?: string;
4
5
  children: ReactNode;
@@ -7,6 +8,7 @@ export interface ModalProps {
7
8
  onClose(): void;
8
9
  contentStyles?: object;
9
10
  withCloseIcon?: boolean;
11
+ focusTrapProps?: Omit<FocusTrapProps, 'children' | 'pause'>;
10
12
  }
11
- export declare const Modal: ({ children, isOpen, contentStyles, withPortal, withCloseIcon, onClose, ...props }: ModalProps) => import("react/jsx-runtime").JSX.Element;
13
+ export declare const Modal: ({ children, isOpen, contentStyles, withPortal, withCloseIcon, onClose, focusTrapProps, ...props }: ModalProps) => import("react/jsx-runtime").JSX.Element;
12
14
  export default Modal;
@@ -1,5 +1,6 @@
1
1
  import { ReactNode } from 'react';
2
2
  import { CSSTransitionProps } from 'react-transition-group/CSSTransition';
3
+ import { FocusTrapProps } from '../FocusTrap';
3
4
  export type DIRECTION = 'left' | 'right';
4
5
  type TransitionProps = Partial<Pick<CSSTransitionProps, 'timeout' | 'classNames' | 'addEndListener' | 'appear' | 'mountOnEnter'>>;
5
6
  export interface OffCanvasProps {
@@ -14,6 +15,7 @@ export interface OffCanvasProps {
14
15
  width?: string;
15
16
  withPortal?: boolean;
16
17
  transitionProps?: TransitionProps;
18
+ focusTrapProps?: Omit<FocusTrapProps, 'children' | 'pause'>;
17
19
  }
18
- export declare const OffCanvas: ({ animationName, animationDuration, backgroundColor, backgroundOverlay, children, from, isOpen, onClickOutside, width, withPortal, transitionProps, }: OffCanvasProps) => import("react/jsx-runtime").JSX.Element;
20
+ export declare const OffCanvas: ({ animationName, animationDuration, backgroundColor, backgroundOverlay, children, from, isOpen, onClickOutside, width, withPortal, transitionProps, focusTrapProps, }: OffCanvasProps) => import("react/jsx-runtime").JSX.Element;
19
21
  export default OffCanvas;