@contentful/f36-popover 5.0.0 → 5.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/README.mdx +8 -0
- package/dist/esm/index.js +5 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/index.d.mts +481 -0
- package/dist/index.js +10 -11
- package/dist/index.js.map +1 -1
- package/package.json +8 -6
package/README.mdx
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
title: 'Popover'
|
|
3
|
+
type: 'component'
|
|
4
|
+
status: 'stable'
|
|
3
5
|
slug: /components/popover/
|
|
4
6
|
github: 'https://github.com/contentful/forma-36/tree/main/packages/components/popover'
|
|
5
7
|
storybook: 'https://f36-storybook.contentful.com/?path=/story/components-popover--basic'
|
|
@@ -53,6 +55,12 @@ consider using [react-focus-lock](https://github.com/theKashey/react-focus-lock)
|
|
|
53
55
|
|
|
54
56
|
- Use an interactive element such as `button` for `Popover.Trigger`
|
|
55
57
|
|
|
58
|
+
<Note variant="neutral">
|
|
59
|
+
We strongly recommend using an interactive element such as "button" for
|
|
60
|
+
"Popover.Trigger", but if you need to use a non-interactive element, like
|
|
61
|
+
"div", you should make that element focusable by providing "tabindex='0'".
|
|
62
|
+
</Note>
|
|
63
|
+
|
|
56
64
|
## Accessibility
|
|
57
65
|
|
|
58
66
|
- If the popover contains interactive elements that user can navigate through with `Tab`, consider using [react-focus-lock](https://github.com/theKashey/react-focus-lock) to trap the focus within Popover
|
package/dist/esm/index.js
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import
|
|
1
|
+
import L, { useState, useEffect, useCallback, useRef, useMemo } from 'react';
|
|
2
2
|
import { useId, mergeRefs } from '@contentful/f36-core';
|
|
3
3
|
import { usePopper } from 'react-popper';
|
|
4
4
|
import { cx, css } from 'emotion';
|
|
5
5
|
import { Portal } from '@contentful/f36-utils';
|
|
6
|
-
import
|
|
6
|
+
import x from '@contentful/f36-tokens';
|
|
7
7
|
|
|
8
|
-
var
|
|
8
|
+
var te=Object.defineProperty,re=Object.defineProperties;var ne=Object.getOwnPropertyDescriptors;var y=Object.getOwnPropertySymbols;var k=Object.prototype.hasOwnProperty,A=Object.prototype.propertyIsEnumerable;var F=(e,t,o)=>t in e?te(e,t,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[t]=o,a=(e,t)=>{for(var o in t||(t={}))k.call(t,o)&&F(e,o,t[o]);if(y)for(var o of y(t))A.call(t,o)&&F(e,o,t[o]);return e},m=(e,t)=>re(e,ne(t));var N=(e,t)=>{var o={};for(var r in e)k.call(e,r)&&t.indexOf(r)<0&&(o[r]=e[r]);if(e!=null&&y)for(var r of y(e))t.indexOf(r)<0&&A.call(e,r)&&(o[r]=e[r]);return o};var K=(e,t,o)=>new Promise((r,f)=>{var v=s=>{try{l(o.next(s));}catch(n){f(n);}},P=s=>{try{l(o.throw(s));}catch(n){f(n);}},l=s=>s.done?r(s.value):Promise.resolve(s.value).then(v,P);l((o=o.apply(e,t)).next());});var U=L.createContext(void 0),h=()=>{let e=L.useContext(U);if(e===void 0)throw new Error("usePopoverContext must be used within a PopoverContextProvider");return e},$=U.Provider;function V(e){let{children:t,isOpen:o,placement:r="bottom-start",isFullWidth:f=!1,isAutoalignmentEnabled:v=!0,usePortal:P=!0,closeOnBlur:l=!0,closeOnEsc:s=!0,onClose:n,autoFocus:C=!0,id:E,offset:T=[1,4],renderOnlyWhenOpen:g=!0}=e,[i,X]=useState(null),[c,Y]=useState(null),{attributes:S,update:M,styles:I}=usePopper(i,c,{placement:r,modifiers:[{name:"offset",options:{offset:T}},m(a({},ue),{enabled:f}),{name:"preventOverflow",enabled:v,options:{mainAxis:!0}},{name:"flip",enabled:v}]});useEffect(()=>{o&&C&&c&&c.focus({preventScroll:!0});},[o,c]),useEffect(()=>{(()=>K(this,null,function*(){o&&M&&(yield M());}))();},[o,M]);let Z=useId(void 0,"popover-content"),w=E||Z,B=useCallback(()=>{n==null||n(),setTimeout(()=>i==null?void 0:i.focus({preventScroll:!0}),0);},[n,i]),O=useRef(!1),_=useMemo(()=>({isOpen:!!o,usePortal:P,renderOnlyWhenOpen:g,getTriggerProps:(p={},H=null)=>({onMouseDown:u=>{var d;O.current=!0,(d=p.onMouseDown)==null||d.call(p,u);},onMouseUp:u=>{var d;O.current=!1,(d=p.onMouseUp)==null||d.call(p,u);},ref:mergeRefs(X,H),"aria-expanded":!!o,"aria-controls":w}),getPopoverProps:(p={},H=null)=>m(a({},S.popper),{style:a(a({},p.style||{}),I.popper),ref:mergeRefs(Y,H),id:w,onBlur:u=>{if(p.onBlur&&p.onBlur(u),!l)return;let d=document.activeElement,b=u.relatedTarget||d,ee=c===b||(c==null?void 0:c.contains(b)),oe=i===b||(i==null?void 0:i.contains(b))||O.current;ee||oe||n==null||n();},onKeyDown:u=>{p.onKeyDown&&p.onKeyDown(u),s&&u.key==="Escape"&&B();}})}),[o,g,S,I,P,w,s,l,c,i,B,n]);return L.createElement($,{value:_},t)}var ue={name:"sameWidth",enabled:!0,phase:"beforeWrite",requires:["computeStyles"],fn:({state:e})=>{e.styles.popper.width=`${e.rects.reference.width}px`;},effect:({state:e})=>()=>{let t=e.elements.reference;e.elements.popper.style.width=`${t.offsetWidth}px`;}};var J=e=>({container:css({display:e?"initial":"none",background:x.colorWhite,border:0,borderRadius:x.borderRadiusMedium,boxShadow:x.boxShadowDefault,zIndex:x.zIndexDropdown,"&:focus":{boxShadow:x.glowPrimary,outline:"none"},"&:focus:not(:focus-visible)":{boxShadow:x.boxShadowDefault}})});var fe=(e,t)=>{let g=e,{children:o,className:r,testId:f="cf-ui-popover-content",role:v="dialog"}=g,P=N(g,["children","className","testId","role"]),{isOpen:l,renderOnlyWhenOpen:s,getPopoverProps:n,usePortal:C}=h(),E=J(l),T=L.createElement("div",m(a(a({},P),n(P,t)),{className:cx(E.container,r),"data-test-id":f,tabIndex:-1,role:v,"data-position-absolute":!0}),o);return s&&!l?null:C?L.createElement(Portal,null,T):T},R=L.forwardRef(fe);var D=e=>{var r;let t=L.Children.only(e.children),{getTriggerProps:o}=h();return L.cloneElement(t,m(a({},o(t.props,t.ref)),{"aria-haspopup":(r=t.props["aria-haspopup"])!=null?r:"dialog"}))};var W=V;W.Content=R;W.Trigger=D;
|
|
9
9
|
|
|
10
|
-
export {
|
|
10
|
+
export { W as Popover, R as PopoverContent, D as PopoverTrigger };
|
|
11
|
+
//# sourceMappingURL=out.js.map
|
|
11
12
|
//# sourceMappingURL=index.js.map
|
package/dist/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/Popover.tsx","../../src/PopoverContext.ts","../../src/PopoverContent/PopoverContent.tsx","../../src/PopoverContent/PopoverContent.styles.ts","../../src/PopoverTrigger/PopoverTrigger.tsx","../../src/CompoundPopover.tsx"],"names":["React","useMemo","useState","useEffect","useCallback","useId","mergeRefs","PopoverContext","usePopoverContext","context","PopoverContextProvider","usePopper","Popover","props","children","isOpen","placement","isFullWidth","isAutoalignmentEnabled","usePortal","closeOnBlur","closeOnEsc","onClose","autoFocus","id","offset","renderOnlyWhenOpen","triggerElement","setTriggerElement","popoverElement","setPopoverElement","popperAttributes","update","popperStyles","__spreadProps","__spreadValues","sameWidth","__async","popoverGeneratedId","popoverId","closeAndFocusTrigger","contextValue","_ref","_props","event","relatedTarget","targetIsPopover","targetIsTrigger","state","reference","cx","Portal","css","tokens","getPopoverContentStyles","_PopoverContent","ref","_a","className","testId","role","otherProps","__objRest","getPopoverProps","styles","content","PopoverContent","PopoverTrigger","child","getTriggerProps"],"mappings":"kyBAAA,OAAOA,IAAS,WAAAC,GAAS,YAAAC,EAAU,aAAAC,EAAW,eAAAC,OAAmB,QACjE,OAAS,SAAAC,GAAO,aAAAC,MAAmC,uBCDnD,OAAON,MAA0B,QAajC,IAAMO,EAAiBP,EAAM,cAC3B,MACF,EAEaQ,EAAoB,IAAM,CACrC,IAAMC,EAAUT,EAAM,WAAWO,CAAc,EAE/C,G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AAA4C,EAWE,IAAMW,EAAQpE,EAAM,SAAS,KAAKa,EAAM,QAAQ,EAC1C,CAAE,gBAAAwD,CAAgB,EAAI7D,EAAkB,EAE9C,OAAOR,EAAM,aAAaoE,EAAOlC,EAAAC,EAAA,GAC5BkC,EAAgBD,EAAM,GAAG,GADG,CAE/B,iBAAiBX,EAAAW,EAAM,MAAM,mBAAZ,KAAAX,EAAgC,QACnD,EAAC,CACH,ECTO,IAAM7C,EAAUA,EACvBA,EAAQ,QAAUsD,EAClBtD,EAAQ,QAAUuD","sourcesContent":["import React, { useMemo, useState, useEffect, useCallback } from 'react';\nimport { useId, mergeRefs, type ExpandProps } from '@contentful/f36-core';\nimport type { Placement, Modifier } from '@popperjs/core';\nimport { PopoverContextProvider, PopoverContextType } from './PopoverContext';\nimport { usePopper } from 'react-popper';\n\nexport interface PopoverProps {\n children: React.ReactNode;\n\n /**\n * Boolean to determine if the Popover should be the same width as\n * the trigger element\n *\n * @default false\n */\n isFullWidth?: boolean;\n\n /**\n * Boolean to control whether or not the Popover is open\n *\n * @default false\n */\n isOpen?: boolean;\n\n /**\n * Callback fired when the popover closes\n */\n onClose?: () => void;\n\n /**\n * Determines the preferred position of the Popover. This position is not\n * guaranteed, as the Popover might be moved to fit the viewport\n *\n * @default bottom-start\n */\n placement?: Placement;\n\n /**\n * Boolean to control if popover is allowed to change its placement automatically\n * based on available space in the viewport.\n *\n * For example:\n * If you set placement prop to bottom, but there isn't enough space to position the popover in that direction,\n * it will change the popper placement to top. As soon as enough space is detected, the placement will be reverted to the defined one.\n *\n * If you want the popover to strictly follow the placement prop you should set this prop to false.\n *\n * @default true\n */\n isAutoalignmentEnabled?: boolean;\n\n /**\n * Boolean to control whether or not to render the Popover in a React Portal.\n * Rendering content inside a Portal allows the Popover to escape the bounds\n * of its parent while still being positioned correctly. Using a Portal is\n * necessary if an ancestor of the Popover hides overflow.\n *\n * @default true\n */\n usePortal?: boolean;\n\n /**\n * If true, the popover will close when you blur out it by clicking outside or tabbing out\n *\n * @default true\n */\n closeOnBlur?: boolean;\n\n /**\n * If true, the popover will close when you hit the Esc key\n *\n * @default true\n */\n closeOnEsc?: boolean;\n\n /**\n * If true, the popover will be focused after opening\n *\n * @default true\n */\n autoFocus?: boolean;\n\n /**\n * Popover id. Will be used as an `id` attribute on popover\n * and as `aria-controls` attribute on trigger\n *\n * @default true\n */\n id?: string;\n\n /**\n * The `X-axis` and `Y-axis` offset to position popper element\n * from its trigger element. `[X, Y]`\n *\n * @default [1, 4]\n */\n offset?: [number, number];\n\n /**\n * Defines if popover should be rendered in the DOM only when it's open\n * or all the time (after the component has been mounted)\n *\n * @default true\n */\n renderOnlyWhenOpen?: boolean;\n}\n\nexport function Popover(props: ExpandProps<PopoverProps>) {\n const {\n children,\n isOpen,\n placement = 'bottom-start',\n isFullWidth = false,\n isAutoalignmentEnabled = true,\n usePortal = true,\n closeOnBlur = true,\n closeOnEsc = true,\n onClose,\n autoFocus = true,\n id,\n offset = [1, 4],\n renderOnlyWhenOpen = true,\n } = props;\n\n const [triggerElement, setTriggerElement] = useState<HTMLElement | null>(\n null,\n );\n const [popoverElement, setPopoverElement] = useState<HTMLElement | null>(\n null,\n );\n\n const {\n attributes: popperAttributes,\n update,\n styles: popperStyles,\n } = usePopper(triggerElement, popoverElement, {\n placement,\n modifiers: [\n {\n name: 'offset',\n options: {\n offset,\n },\n },\n {\n ...sameWidth,\n enabled: isFullWidth,\n },\n {\n name: 'preventOverflow',\n enabled: isAutoalignmentEnabled,\n options: {\n mainAxis: true,\n },\n },\n {\n name: 'flip',\n enabled: isAutoalignmentEnabled,\n },\n ],\n });\n\n useEffect(() => {\n if (isOpen && autoFocus && popoverElement) {\n popoverElement.focus({ preventScroll: true });\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [isOpen, popoverElement]);\n\n useEffect(() => {\n const updatePosition = async () => {\n if (isOpen && update) {\n await update();\n }\n };\n updatePosition();\n }, [isOpen, update]);\n\n const popoverGeneratedId = useId(undefined, 'popover-content');\n const popoverId = id || popoverGeneratedId;\n\n const closeAndFocusTrigger = useCallback(() => {\n onClose?.();\n\n // setTimeout trick to make it work with focus-lock\n setTimeout(() => triggerElement?.focus({ preventScroll: true }), 0);\n }, [onClose, triggerElement]);\n\n const contextValue: PopoverContextType = useMemo(\n () => ({\n isOpen: Boolean(isOpen),\n usePortal,\n renderOnlyWhenOpen,\n getTriggerProps: (_ref = null) => ({\n ref: mergeRefs(setTriggerElement, _ref),\n ['aria-expanded']: Boolean(isOpen),\n ['aria-controls']: popoverId,\n }),\n getPopoverProps: (_props = {}, _ref = null) => ({\n ...popperAttributes.popper,\n style: {\n ...(_props.style || {}),\n ...popperStyles.popper,\n },\n ref: mergeRefs(setPopoverElement, _ref),\n id: popoverId,\n onBlur: (event: React.FocusEvent<HTMLDivElement>) => {\n if (_props.onBlur) {\n _props.onBlur(event);\n }\n\n if (!closeOnBlur) {\n return;\n }\n\n const relatedTarget = event.relatedTarget as Node;\n\n const targetIsPopover =\n popoverElement === relatedTarget ||\n popoverElement?.contains(relatedTarget);\n const targetIsTrigger =\n triggerElement === relatedTarget ||\n triggerElement?.contains(relatedTarget);\n\n if (targetIsPopover || targetIsTrigger) {\n return;\n }\n\n onClose?.();\n },\n onKeyDown: (event: React.KeyboardEvent<HTMLDivElement>) => {\n if (_props.onKeyDown) {\n _props.onKeyDown(event);\n }\n\n if (closeOnEsc && event.key === 'Escape') {\n closeAndFocusTrigger();\n }\n },\n }),\n }),\n [\n isOpen,\n renderOnlyWhenOpen,\n popperAttributes,\n popperStyles,\n usePortal,\n popoverId,\n closeOnEsc,\n closeOnBlur,\n popoverElement,\n triggerElement,\n closeAndFocusTrigger,\n onClose,\n ],\n );\n\n return (\n <PopoverContextProvider value={contextValue}>\n {children}\n </PopoverContextProvider>\n );\n}\n\n/**\n * Sets the popover width to the size of the trigger element.\n */\nconst sameWidth: Modifier<'sameWidth', any> = {\n name: 'sameWidth',\n enabled: true,\n phase: 'beforeWrite',\n requires: ['computeStyles'],\n fn: ({ state }) => {\n state.styles.popper.width = `${state.rects.reference.width}px`;\n },\n effect:\n ({ state }) =>\n () => {\n const reference = state.elements.reference as HTMLElement;\n state.elements.popper.style.width = `${reference.offsetWidth}px`;\n },\n};\n","import React, { HTMLProps } from 'react';\n\nexport type PopoverContextType = {\n isOpen: boolean;\n usePortal: boolean;\n renderOnlyWhenOpen: boolean;\n getPopoverProps: (\n _props: HTMLProps<HTMLDivElement>,\n _ref: React.Ref<HTMLDivElement>,\n ) => HTMLProps<HTMLDivElement>;\n getTriggerProps: (_ref: React.Ref<HTMLElement>) => HTMLProps<HTMLElement>;\n};\n\nconst PopoverContext = React.createContext<PopoverContextType | undefined>(\n undefined,\n);\n\nexport const usePopoverContext = () => {\n const context = React.useContext(PopoverContext);\n\n if (context === undefined) {\n throw new Error(\n 'usePopoverContext must be used within a PopoverContextProvider',\n );\n }\n\n return context;\n};\n\nexport const PopoverContextProvider = PopoverContext.Provider;\n","import React from 'react';\nimport { cx } from 'emotion';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\nimport { usePopoverContext } from '../PopoverContext';\nimport { Portal } from '@contentful/f36-utils';\nimport { getPopoverContentStyles } from './PopoverContent.styles';\n\ninterface PopoverContentInternalProps extends CommonProps {\n children?: React.ReactNode;\n}\n\nexport type PopoverContentProps = PropsWithHTMLElement<\n PopoverContentInternalProps,\n 'div'\n>;\n\nconst _PopoverContent = (props: ExpandProps<PopoverContentProps>, ref) => {\n const {\n children,\n className,\n testId = 'cf-ui-popover-content',\n role = 'dialog',\n ...otherProps\n } = props;\n const { isOpen, renderOnlyWhenOpen, getPopoverProps, usePortal } =\n usePopoverContext();\n\n const styles = getPopoverContentStyles(isOpen);\n\n const content = (\n <div\n {...otherProps}\n {...getPopoverProps(otherProps, ref)}\n className={cx(styles.container, className)}\n data-test-id={testId}\n tabIndex={-1}\n role={role}\n // specific attribute to mark that this element is absolute positioned\n // for internal contentful apps usage\n data-position-absolute\n >\n {children}\n </div>\n );\n\n if (renderOnlyWhenOpen && !isOpen) {\n return null;\n }\n\n return usePortal ? <Portal>{content}</Portal> : content;\n};\n\nexport const PopoverContent = React.forwardRef(_PopoverContent);\n","import { css } from 'emotion';\nimport tokens from '@contentful/f36-tokens';\n\nexport const getPopoverContentStyles = (isOpen: boolean) => ({\n container: css({\n display: isOpen ? 'initial' : 'none',\n background: tokens.colorWhite,\n border: 0,\n borderRadius: tokens.borderRadiusMedium,\n boxShadow: tokens.boxShadowDefault,\n zIndex: tokens.zIndexDropdown,\n '&:focus': {\n boxShadow: tokens.glowPrimary,\n outline: 'none',\n },\n '&:focus:not(:focus-visible)': {\n boxShadow: tokens.boxShadowDefault,\n },\n }),\n});\n","import React from 'react';\nimport { usePopoverContext } from '../PopoverContext';\n\nexport interface PopoverTriggerProps {\n children: React.ReactNode;\n}\n\n/**\n * PopoverTrigger opens the popover. It must be an interactive element.\n */\nexport const PopoverTrigger = (props: PopoverTriggerProps) => {\n const child = React.Children.only(props.children) as any;\n const { getTriggerProps } = usePopoverContext();\n\n return React.cloneElement(child, {\n ...getTriggerProps(child.ref),\n 'aria-haspopup': child.props['aria-haspopup'] ?? 'dialog',\n });\n};\n","import { Popover as OriginalPopover } from './Popover';\nimport { PopoverContent } from './PopoverContent/PopoverContent';\nimport { PopoverTrigger } from './PopoverTrigger/PopoverTrigger';\n\ntype CompoundPopover = typeof OriginalPopover & {\n Content: typeof PopoverContent;\n Trigger: typeof PopoverTrigger;\n};\n\nexport const Popover = OriginalPopover as CompoundPopover;\nPopover.Content = PopoverContent;\nPopover.Trigger = PopoverTrigger;\n"]}
|
|
1
|
+
{"version":3,"sources":["../../src/Popover.tsx","../../src/PopoverContext.ts","../../src/PopoverContent/PopoverContent.tsx","../../src/PopoverContent/PopoverContent.styles.ts","../../src/PopoverTrigger/PopoverTrigger.tsx","../../src/CompoundPopover.tsx"],"names":["React","useMemo","useState","useEffect","useCallback","useRef","useId","mergeRefs","PopoverContext","usePopoverContext","context","PopoverContextProvider","usePopper","Popover","props","children","isOpen","placement","isFullWidth","isAutoalignmentEnabled","usePortal","closeOnBlur","closeOnEsc","onClose","autoFocus","id","offset","renderOnlyWhenOpen","triggerElement","setTriggerElement","popoverElement","setPopoverElement","popperAttributes","update","popperStyles","__spreadProps","__spreadValues","sameWidth","__async","popoverGeneratedId","popoverId","closeAndFocusTrigger","isMouseDown","contextValue","_props","_ref","event","_a","activeElement","relatedTarget","targetIsPopover","targetIsTrigger","state","reference","cx","Portal","css","tokens","getPopoverContentStyles","_PopoverContent","ref","className","testId","role","otherProps","__objRest","getPopoverProps","styles","content","PopoverContent","PopoverTrigger","child","getTriggerProps"],"mappings":"kyBAAA,OAAOA,IACL,WAAAC,GACA,YAAAC,EACA,aAAAC,EACA,eAAAC,GACA,UAAAC,OACK,QACP,OAAS,SAAAC,GAAO,aAAAC,MAAmC,uBCPnD,OAAOP,MAA0B,QAgBjC,IAAMQ,EAAiBR,EAAM,cAC3B,MACF,EAEaS,EAAoB,IAAM,CACrC,IAAMC,EAAUV,EAAM,WAAWQ,CAAc,EAE/C,GAAIE,IAAY,OACd,MAAM,IAAI,MACR,gEACF,EAGF,OAAOA,CACT,EAEaC,EAAyBH,EAAe,SDtBrD,OAAS,aAAAI,OAAiB,eAuGnB,SAASC,EAAQC,EAAkC,CACxD,GAAM,CACJ,SAAAC,EACA,OAAAC,EACA,UAAAC,EAAY,eACZ,YAAAC,EAAc,GACd,uBAAAC,EAAyB,GACzB,UAAAC,EAAY,GACZ,YAAAC,EAAc,GACd,WAAAC,EAAa,GACb,QAAAC,EACA,UAAAC,EAAY,GACZ,GAAAC,EACA,OAAAC,EAAS,CAAC,EAAG,CAAC,EACd,mBAAAC,EAAqB,EACvB,EAAIb,EAEE,CAACc,EAAgBC,CAAiB,EAAI3B,EAC1C,IACF,EACM,CAAC4B,EAAgBC,CAAiB,EAAI7B,EAC1C,IACF,EAEM,CACJ,WAAY8B,EACZ,OAAAC,EACA,OAAQC,CAC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EAAAwB,EAAM,MAAM,eAAe,IAA3B,KAAAxB,EAAgC,QACnD,EAAC,CACH,ECTO,IAAMlC,EAAUA,EACvBA,EAAQ,QAAUwD,EAClBxD,EAAQ,QAAUyD","sourcesContent":["import React, {\n useMemo,\n useState,\n useEffect,\n useCallback,\n useRef,\n} from 'react';\nimport { useId, mergeRefs, type ExpandProps } from '@contentful/f36-core';\nimport type { Placement, Modifier } from '@popperjs/core';\nimport { PopoverContextProvider, PopoverContextType } from './PopoverContext';\nimport { usePopper } from 'react-popper';\n\nexport interface PopoverProps {\n children: React.ReactNode;\n\n /**\n * Boolean to determine if the Popover should be the same width as\n * the trigger element\n *\n * @default false\n */\n isFullWidth?: boolean;\n\n /**\n * Boolean to control whether or not the Popover is open\n *\n * @default false\n */\n isOpen?: boolean;\n\n /**\n * Callback fired when the popover closes\n */\n onClose?: () => void;\n\n /**\n * Determines the preferred position of the Popover. This position is not\n * guaranteed, as the Popover might be moved to fit the viewport\n *\n * @default bottom-start\n */\n placement?: Placement;\n\n /**\n * Boolean to control if popover is allowed to change its placement automatically\n * based on available space in the viewport.\n *\n * For example:\n * If you set placement prop to bottom, but there isn't enough space to position the popover in that direction,\n * it will change the popper placement to top. As soon as enough space is detected, the placement will be reverted to the defined one.\n *\n * If you want the popover to strictly follow the placement prop you should set this prop to false.\n *\n * @default true\n */\n isAutoalignmentEnabled?: boolean;\n\n /**\n * Boolean to control whether or not to render the Popover in a React Portal.\n * Rendering content inside a Portal allows the Popover to escape the bounds\n * of its parent while still being positioned correctly. Using a Portal is\n * necessary if an ancestor of the Popover hides overflow.\n *\n * @default true\n */\n usePortal?: boolean;\n\n /**\n * If true, the popover will close when you blur out it by clicking outside or tabbing out\n *\n * @default true\n */\n closeOnBlur?: boolean;\n\n /**\n * If true, the popover will close when you hit the Esc key\n *\n * @default true\n */\n closeOnEsc?: boolean;\n\n /**\n * If true, the popover will be focused after opening\n *\n * @default true\n */\n autoFocus?: boolean;\n\n /**\n * Popover id. Will be used as an `id` attribute on popover\n * and as `aria-controls` attribute on trigger\n *\n * @default true\n */\n id?: string;\n\n /**\n * The `X-axis` and `Y-axis` offset to position popper element\n * from its trigger element. `[X, Y]`\n *\n * @default [1, 4]\n */\n offset?: [number, number];\n\n /**\n * Defines if popover should be rendered in the DOM only when it's open\n * or all the time (after the component has been mounted)\n *\n * @default true\n */\n renderOnlyWhenOpen?: boolean;\n}\n\nexport function Popover(props: ExpandProps<PopoverProps>) {\n const {\n children,\n isOpen,\n placement = 'bottom-start',\n isFullWidth = false,\n isAutoalignmentEnabled = true,\n usePortal = true,\n closeOnBlur = true,\n closeOnEsc = true,\n onClose,\n autoFocus = true,\n id,\n offset = [1, 4],\n renderOnlyWhenOpen = true,\n } = props;\n\n const [triggerElement, setTriggerElement] = useState<HTMLElement | null>(\n null,\n );\n const [popoverElement, setPopoverElement] = useState<HTMLElement | null>(\n null,\n );\n\n const {\n attributes: popperAttributes,\n update,\n styles: popperStyles,\n } = usePopper(triggerElement, popoverElement, {\n placement,\n modifiers: [\n {\n name: 'offset',\n options: {\n offset,\n },\n },\n {\n ...sameWidth,\n enabled: isFullWidth,\n },\n {\n name: 'preventOverflow',\n enabled: isAutoalignmentEnabled,\n options: {\n mainAxis: true,\n },\n },\n {\n name: 'flip',\n enabled: isAutoalignmentEnabled,\n },\n ],\n });\n\n useEffect(() => {\n if (isOpen && autoFocus && popoverElement) {\n popoverElement.focus({ preventScroll: true });\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [isOpen, popoverElement]);\n\n useEffect(() => {\n const updatePosition = async () => {\n if (isOpen && update) {\n await update();\n }\n };\n updatePosition();\n }, [isOpen, update]);\n\n const popoverGeneratedId = useId(undefined, 'popover-content');\n const popoverId = id || popoverGeneratedId;\n\n const closeAndFocusTrigger = useCallback(() => {\n onClose?.();\n\n // setTimeout trick to make it work with focus-lock\n setTimeout(() => triggerElement?.focus({ preventScroll: true }), 0);\n }, [onClose, triggerElement]);\n\n // Safari has an issue with the relatedTarget that we use on the onBlur for getPopoverProps,\n // which was causing the popover to close and reopen when clicking on the trigger.\n // We will use the isMouseDown to prevent triggering blur in the cases where the user clicks on the trigger.\n const isMouseDown = useRef<Boolean>(false);\n\n const contextValue: PopoverContextType = useMemo(\n () => ({\n isOpen: Boolean(isOpen),\n usePortal,\n renderOnlyWhenOpen,\n getTriggerProps: (_props = {}, _ref = null) => ({\n onMouseDown: (event) => {\n isMouseDown.current = true;\n _props.onMouseDown?.(event);\n },\n onMouseUp: (event) => {\n isMouseDown.current = false;\n _props.onMouseUp?.(event);\n },\n ref: mergeRefs(setTriggerElement, _ref),\n ['aria-expanded']: Boolean(isOpen),\n ['aria-controls']: popoverId,\n }),\n getPopoverProps: (_props = {}, _ref = null) => ({\n ...popperAttributes.popper,\n style: {\n ...(_props.style || {}),\n ...popperStyles.popper,\n },\n ref: mergeRefs(setPopoverElement, _ref),\n id: popoverId,\n onBlur: (event: React.FocusEvent<HTMLDivElement>) => {\n if (_props.onBlur) {\n _props.onBlur(event);\n }\n\n if (!closeOnBlur) {\n return;\n }\n\n const activeElement = document.activeElement;\n const relatedTarget = event.relatedTarget || activeElement;\n\n const targetIsPopover =\n popoverElement === relatedTarget ||\n popoverElement?.contains(relatedTarget);\n const targetIsTrigger =\n triggerElement === relatedTarget ||\n triggerElement?.contains(relatedTarget) ||\n isMouseDown.current;\n\n if (targetIsPopover || targetIsTrigger) {\n return;\n }\n\n onClose?.();\n },\n onKeyDown: (event: React.KeyboardEvent<HTMLDivElement>) => {\n if (_props.onKeyDown) {\n _props.onKeyDown(event);\n }\n\n if (closeOnEsc && event.key === 'Escape') {\n closeAndFocusTrigger();\n }\n },\n }),\n }),\n [\n isOpen,\n renderOnlyWhenOpen,\n popperAttributes,\n popperStyles,\n usePortal,\n popoverId,\n closeOnEsc,\n closeOnBlur,\n popoverElement,\n triggerElement,\n closeAndFocusTrigger,\n onClose,\n ],\n );\n\n return (\n <PopoverContextProvider value={contextValue}>\n {children}\n </PopoverContextProvider>\n );\n}\n\n/**\n * Sets the popover width to the size of the trigger element.\n */\nconst sameWidth: Modifier<'sameWidth', any> = {\n name: 'sameWidth',\n enabled: true,\n phase: 'beforeWrite',\n requires: ['computeStyles'],\n fn: ({ state }) => {\n state.styles.popper.width = `${state.rects.reference.width}px`;\n },\n effect:\n ({ state }) =>\n () => {\n const reference = state.elements.reference as HTMLElement;\n state.elements.popper.style.width = `${reference.offsetWidth}px`;\n },\n};\n","import React, { HTMLProps } from 'react';\n\nexport type PopoverContextType = {\n isOpen: boolean;\n usePortal: boolean;\n renderOnlyWhenOpen: boolean;\n getPopoverProps: (\n _props: HTMLProps<HTMLDivElement>,\n _ref: React.Ref<HTMLDivElement>,\n ) => HTMLProps<HTMLDivElement>;\n getTriggerProps: (\n _props: HTMLProps<HTMLElement>,\n _ref: React.Ref<HTMLElement>,\n ) => HTMLProps<HTMLElement>;\n};\n\nconst PopoverContext = React.createContext<PopoverContextType | undefined>(\n undefined,\n);\n\nexport const usePopoverContext = () => {\n const context = React.useContext(PopoverContext);\n\n if (context === undefined) {\n throw new Error(\n 'usePopoverContext must be used within a PopoverContextProvider',\n );\n }\n\n return context;\n};\n\nexport const PopoverContextProvider = PopoverContext.Provider;\n","import React from 'react';\nimport { cx } from 'emotion';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\nimport { usePopoverContext } from '../PopoverContext';\nimport { Portal } from '@contentful/f36-utils';\nimport { getPopoverContentStyles } from './PopoverContent.styles';\n\ninterface PopoverContentInternalProps extends CommonProps {\n children?: React.ReactNode;\n}\n\nexport type PopoverContentProps = PropsWithHTMLElement<\n PopoverContentInternalProps,\n 'div'\n>;\n\nconst _PopoverContent = (props: ExpandProps<PopoverContentProps>, ref) => {\n const {\n children,\n className,\n testId = 'cf-ui-popover-content',\n role = 'dialog',\n ...otherProps\n } = props;\n const { isOpen, renderOnlyWhenOpen, getPopoverProps, usePortal } =\n usePopoverContext();\n\n const styles = getPopoverContentStyles(isOpen);\n\n const content = (\n <div\n {...otherProps}\n {...getPopoverProps(otherProps, ref)}\n className={cx(styles.container, className)}\n data-test-id={testId}\n tabIndex={-1}\n role={role}\n // specific attribute to mark that this element is absolute positioned\n // for internal contentful apps usage\n data-position-absolute\n >\n {children}\n </div>\n );\n\n if (renderOnlyWhenOpen && !isOpen) {\n return null;\n }\n\n return usePortal ? <Portal>{content}</Portal> : content;\n};\n\nexport const PopoverContent = React.forwardRef(_PopoverContent);\n","import { css } from 'emotion';\nimport tokens from '@contentful/f36-tokens';\n\nexport const getPopoverContentStyles = (isOpen: boolean) => ({\n container: css({\n display: isOpen ? 'initial' : 'none',\n background: tokens.colorWhite,\n border: 0,\n borderRadius: tokens.borderRadiusMedium,\n boxShadow: tokens.boxShadowDefault,\n zIndex: tokens.zIndexDropdown,\n '&:focus': {\n boxShadow: tokens.glowPrimary,\n outline: 'none',\n },\n '&:focus:not(:focus-visible)': {\n boxShadow: tokens.boxShadowDefault,\n },\n }),\n});\n","import React from 'react';\nimport { usePopoverContext } from '../PopoverContext';\n\nexport interface PopoverTriggerProps {\n children: React.ReactNode;\n}\n\n/**\n * PopoverTrigger opens the popover. It must be an interactive element.\n */\nexport const PopoverTrigger = (props: PopoverTriggerProps) => {\n const child = React.Children.only(props.children) as any;\n const { getTriggerProps } = usePopoverContext();\n\n return React.cloneElement(child, {\n ...getTriggerProps(child.props, child.ref),\n 'aria-haspopup': child.props['aria-haspopup'] ?? 'dialog',\n });\n};\n","import { Popover as OriginalPopover } from './Popover';\nimport { PopoverContent } from './PopoverContent/PopoverContent';\nimport { PopoverTrigger } from './PopoverTrigger/PopoverTrigger';\n\ntype CompoundPopover = typeof OriginalPopover & {\n Content: typeof PopoverContent;\n Trigger: typeof PopoverTrigger;\n};\n\nexport const Popover = OriginalPopover as CompoundPopover;\nPopover.Content = PopoverContent;\nPopover.Trigger = PopoverTrigger;\n"]}
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,481 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { ExpandProps, PropsWithHTMLElement, CommonProps } from '@contentful/f36-core';
|
|
3
|
+
import { Placement } from '@popperjs/core';
|
|
4
|
+
import * as _emotion_core from '@emotion/core';
|
|
5
|
+
|
|
6
|
+
interface PopoverProps {
|
|
7
|
+
children: React.ReactNode;
|
|
8
|
+
/**
|
|
9
|
+
* Boolean to determine if the Popover should be the same width as
|
|
10
|
+
* the trigger element
|
|
11
|
+
*
|
|
12
|
+
* @default false
|
|
13
|
+
*/
|
|
14
|
+
isFullWidth?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* Boolean to control whether or not the Popover is open
|
|
17
|
+
*
|
|
18
|
+
* @default false
|
|
19
|
+
*/
|
|
20
|
+
isOpen?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Callback fired when the popover closes
|
|
23
|
+
*/
|
|
24
|
+
onClose?: () => void;
|
|
25
|
+
/**
|
|
26
|
+
* Determines the preferred position of the Popover. This position is not
|
|
27
|
+
* guaranteed, as the Popover might be moved to fit the viewport
|
|
28
|
+
*
|
|
29
|
+
* @default bottom-start
|
|
30
|
+
*/
|
|
31
|
+
placement?: Placement;
|
|
32
|
+
/**
|
|
33
|
+
* Boolean to control if popover is allowed to change its placement automatically
|
|
34
|
+
* based on available space in the viewport.
|
|
35
|
+
*
|
|
36
|
+
* For example:
|
|
37
|
+
* If you set placement prop to bottom, but there isn't enough space to position the popover in that direction,
|
|
38
|
+
* it will change the popper placement to top. As soon as enough space is detected, the placement will be reverted to the defined one.
|
|
39
|
+
*
|
|
40
|
+
* If you want the popover to strictly follow the placement prop you should set this prop to false.
|
|
41
|
+
*
|
|
42
|
+
* @default true
|
|
43
|
+
*/
|
|
44
|
+
isAutoalignmentEnabled?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Boolean to control whether or not to render the Popover in a React Portal.
|
|
47
|
+
* Rendering content inside a Portal allows the Popover to escape the bounds
|
|
48
|
+
* of its parent while still being positioned correctly. Using a Portal is
|
|
49
|
+
* necessary if an ancestor of the Popover hides overflow.
|
|
50
|
+
*
|
|
51
|
+
* @default true
|
|
52
|
+
*/
|
|
53
|
+
usePortal?: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* If true, the popover will close when you blur out it by clicking outside or tabbing out
|
|
56
|
+
*
|
|
57
|
+
* @default true
|
|
58
|
+
*/
|
|
59
|
+
closeOnBlur?: boolean;
|
|
60
|
+
/**
|
|
61
|
+
* If true, the popover will close when you hit the Esc key
|
|
62
|
+
*
|
|
63
|
+
* @default true
|
|
64
|
+
*/
|
|
65
|
+
closeOnEsc?: boolean;
|
|
66
|
+
/**
|
|
67
|
+
* If true, the popover will be focused after opening
|
|
68
|
+
*
|
|
69
|
+
* @default true
|
|
70
|
+
*/
|
|
71
|
+
autoFocus?: boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Popover id. Will be used as an `id` attribute on popover
|
|
74
|
+
* and as `aria-controls` attribute on trigger
|
|
75
|
+
*
|
|
76
|
+
* @default true
|
|
77
|
+
*/
|
|
78
|
+
id?: string;
|
|
79
|
+
/**
|
|
80
|
+
* The `X-axis` and `Y-axis` offset to position popper element
|
|
81
|
+
* from its trigger element. `[X, Y]`
|
|
82
|
+
*
|
|
83
|
+
* @default [1, 4]
|
|
84
|
+
*/
|
|
85
|
+
offset?: [number, number];
|
|
86
|
+
/**
|
|
87
|
+
* Defines if popover should be rendered in the DOM only when it's open
|
|
88
|
+
* or all the time (after the component has been mounted)
|
|
89
|
+
*
|
|
90
|
+
* @default true
|
|
91
|
+
*/
|
|
92
|
+
renderOnlyWhenOpen?: boolean;
|
|
93
|
+
}
|
|
94
|
+
declare function Popover$1(props: ExpandProps<PopoverProps>): JSX.Element;
|
|
95
|
+
|
|
96
|
+
interface PopoverContentInternalProps extends CommonProps {
|
|
97
|
+
children?: React.ReactNode;
|
|
98
|
+
}
|
|
99
|
+
declare type PopoverContentProps = PropsWithHTMLElement<PopoverContentInternalProps, 'div'>;
|
|
100
|
+
declare const PopoverContent: React.ForwardRefExoticComponent<Omit<Omit<Pick<React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "key" | keyof React.HTMLAttributes<HTMLDivElement>>, never>, keyof PopoverContentInternalProps> & PopoverContentInternalProps & React.RefAttributes<unknown>>;
|
|
101
|
+
|
|
102
|
+
interface PopoverTriggerProps {
|
|
103
|
+
children: React.ReactNode;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* PopoverTrigger opens the popover. It must be an interactive element.
|
|
107
|
+
*/
|
|
108
|
+
declare const PopoverTrigger: (props: PopoverTriggerProps) => React.DetailedReactHTMLElement<{
|
|
109
|
+
'aria-haspopup': any;
|
|
110
|
+
accept?: string;
|
|
111
|
+
acceptCharset?: string;
|
|
112
|
+
action?: string;
|
|
113
|
+
allowFullScreen?: boolean;
|
|
114
|
+
allowTransparency?: boolean;
|
|
115
|
+
alt?: string;
|
|
116
|
+
as?: string;
|
|
117
|
+
async?: boolean;
|
|
118
|
+
autoComplete?: string;
|
|
119
|
+
autoFocus?: boolean;
|
|
120
|
+
autoPlay?: boolean;
|
|
121
|
+
capture?: string | boolean;
|
|
122
|
+
cellPadding?: string | number;
|
|
123
|
+
cellSpacing?: string | number;
|
|
124
|
+
charSet?: string;
|
|
125
|
+
challenge?: string;
|
|
126
|
+
checked?: boolean;
|
|
127
|
+
cite?: string;
|
|
128
|
+
classID?: string;
|
|
129
|
+
cols?: number;
|
|
130
|
+
colSpan?: number;
|
|
131
|
+
content?: string;
|
|
132
|
+
controls?: boolean;
|
|
133
|
+
coords?: string;
|
|
134
|
+
crossOrigin?: string;
|
|
135
|
+
data?: string;
|
|
136
|
+
dateTime?: string;
|
|
137
|
+
default?: boolean;
|
|
138
|
+
defer?: boolean;
|
|
139
|
+
disabled?: boolean;
|
|
140
|
+
download?: any;
|
|
141
|
+
encType?: string;
|
|
142
|
+
form?: string;
|
|
143
|
+
formAction?: string;
|
|
144
|
+
formEncType?: string;
|
|
145
|
+
formMethod?: string;
|
|
146
|
+
formNoValidate?: boolean;
|
|
147
|
+
formTarget?: string;
|
|
148
|
+
frameBorder?: string | number;
|
|
149
|
+
headers?: string;
|
|
150
|
+
height?: string | number;
|
|
151
|
+
high?: number;
|
|
152
|
+
href?: string;
|
|
153
|
+
hrefLang?: string;
|
|
154
|
+
htmlFor?: string;
|
|
155
|
+
httpEquiv?: string;
|
|
156
|
+
integrity?: string;
|
|
157
|
+
keyParams?: string;
|
|
158
|
+
keyType?: string;
|
|
159
|
+
kind?: string;
|
|
160
|
+
label?: string;
|
|
161
|
+
list?: string;
|
|
162
|
+
loop?: boolean;
|
|
163
|
+
low?: number;
|
|
164
|
+
manifest?: string;
|
|
165
|
+
marginHeight?: number;
|
|
166
|
+
marginWidth?: number;
|
|
167
|
+
max?: string | number;
|
|
168
|
+
maxLength?: number;
|
|
169
|
+
media?: string;
|
|
170
|
+
mediaGroup?: string;
|
|
171
|
+
method?: string;
|
|
172
|
+
min?: string | number;
|
|
173
|
+
minLength?: number;
|
|
174
|
+
multiple?: boolean;
|
|
175
|
+
muted?: boolean;
|
|
176
|
+
name?: string;
|
|
177
|
+
nonce?: string;
|
|
178
|
+
noValidate?: boolean;
|
|
179
|
+
open?: boolean;
|
|
180
|
+
optimum?: number;
|
|
181
|
+
pattern?: string;
|
|
182
|
+
placeholder?: string;
|
|
183
|
+
playsInline?: boolean;
|
|
184
|
+
poster?: string;
|
|
185
|
+
preload?: string;
|
|
186
|
+
readOnly?: boolean;
|
|
187
|
+
rel?: string;
|
|
188
|
+
required?: boolean;
|
|
189
|
+
reversed?: boolean;
|
|
190
|
+
rows?: number;
|
|
191
|
+
rowSpan?: number;
|
|
192
|
+
sandbox?: string;
|
|
193
|
+
scope?: string;
|
|
194
|
+
scoped?: boolean;
|
|
195
|
+
scrolling?: string;
|
|
196
|
+
seamless?: boolean;
|
|
197
|
+
selected?: boolean;
|
|
198
|
+
shape?: string;
|
|
199
|
+
size?: number;
|
|
200
|
+
sizes?: string;
|
|
201
|
+
span?: number;
|
|
202
|
+
src?: string;
|
|
203
|
+
srcDoc?: string;
|
|
204
|
+
srcLang?: string;
|
|
205
|
+
srcSet?: string;
|
|
206
|
+
start?: number;
|
|
207
|
+
step?: string | number;
|
|
208
|
+
summary?: string;
|
|
209
|
+
target?: string;
|
|
210
|
+
type?: string;
|
|
211
|
+
useMap?: string;
|
|
212
|
+
value?: string | number | readonly string[];
|
|
213
|
+
width?: string | number;
|
|
214
|
+
wmode?: string;
|
|
215
|
+
wrap?: string;
|
|
216
|
+
defaultChecked?: boolean;
|
|
217
|
+
defaultValue?: string | number | readonly string[];
|
|
218
|
+
suppressContentEditableWarning?: boolean;
|
|
219
|
+
suppressHydrationWarning?: boolean;
|
|
220
|
+
accessKey?: string;
|
|
221
|
+
className?: string;
|
|
222
|
+
contentEditable?: "inherit" | (boolean | "false" | "true");
|
|
223
|
+
contextMenu?: string;
|
|
224
|
+
dir?: string;
|
|
225
|
+
draggable?: boolean | "false" | "true";
|
|
226
|
+
hidden?: boolean;
|
|
227
|
+
id?: string;
|
|
228
|
+
lang?: string;
|
|
229
|
+
slot?: string;
|
|
230
|
+
spellCheck?: boolean | "false" | "true";
|
|
231
|
+
style?: React.CSSProperties;
|
|
232
|
+
tabIndex?: number;
|
|
233
|
+
title?: string;
|
|
234
|
+
translate?: "yes" | "no";
|
|
235
|
+
radioGroup?: string;
|
|
236
|
+
role?: React.AriaRole;
|
|
237
|
+
about?: string;
|
|
238
|
+
datatype?: string;
|
|
239
|
+
inlist?: any;
|
|
240
|
+
prefix?: string;
|
|
241
|
+
property?: string;
|
|
242
|
+
resource?: string;
|
|
243
|
+
typeof?: string;
|
|
244
|
+
vocab?: string;
|
|
245
|
+
autoCapitalize?: string;
|
|
246
|
+
autoCorrect?: string;
|
|
247
|
+
autoSave?: string;
|
|
248
|
+
color?: string;
|
|
249
|
+
itemProp?: string;
|
|
250
|
+
itemScope?: boolean;
|
|
251
|
+
itemType?: string;
|
|
252
|
+
itemID?: string;
|
|
253
|
+
itemRef?: string;
|
|
254
|
+
results?: number;
|
|
255
|
+
security?: string;
|
|
256
|
+
unselectable?: "on" | "off";
|
|
257
|
+
inputMode?: "none" | "text" | "search" | "tel" | "url" | "email" | "numeric" | "decimal";
|
|
258
|
+
is?: string;
|
|
259
|
+
'aria-activedescendant'?: string;
|
|
260
|
+
'aria-atomic'?: boolean | "false" | "true";
|
|
261
|
+
'aria-autocomplete'?: "both" | "none" | "inline" | "list";
|
|
262
|
+
'aria-busy'?: boolean | "false" | "true";
|
|
263
|
+
'aria-checked'?: boolean | "false" | "true" | "mixed";
|
|
264
|
+
'aria-colcount'?: number;
|
|
265
|
+
'aria-colindex'?: number;
|
|
266
|
+
'aria-colspan'?: number;
|
|
267
|
+
'aria-controls'?: string;
|
|
268
|
+
'aria-current'?: boolean | "false" | "true" | "page" | "time" | "step" | "location" | "date";
|
|
269
|
+
'aria-describedby'?: string;
|
|
270
|
+
'aria-details'?: string;
|
|
271
|
+
'aria-disabled'?: boolean | "false" | "true";
|
|
272
|
+
'aria-dropeffect'?: "none" | "copy" | "move" | "link" | "execute" | "popup";
|
|
273
|
+
'aria-errormessage'?: string;
|
|
274
|
+
'aria-expanded'?: boolean | "false" | "true";
|
|
275
|
+
'aria-flowto'?: string;
|
|
276
|
+
'aria-grabbed'?: boolean | "false" | "true";
|
|
277
|
+
'aria-hidden'?: boolean | "false" | "true";
|
|
278
|
+
'aria-invalid'?: boolean | "false" | "true" | "grammar" | "spelling";
|
|
279
|
+
'aria-keyshortcuts'?: string;
|
|
280
|
+
'aria-label'?: string;
|
|
281
|
+
'aria-labelledby'?: string;
|
|
282
|
+
'aria-level'?: number;
|
|
283
|
+
'aria-live'?: "off" | "assertive" | "polite";
|
|
284
|
+
'aria-modal'?: boolean | "false" | "true";
|
|
285
|
+
'aria-multiline'?: boolean | "false" | "true";
|
|
286
|
+
'aria-multiselectable'?: boolean | "false" | "true";
|
|
287
|
+
'aria-orientation'?: "horizontal" | "vertical";
|
|
288
|
+
'aria-owns'?: string;
|
|
289
|
+
'aria-placeholder'?: string;
|
|
290
|
+
'aria-posinset'?: number;
|
|
291
|
+
'aria-pressed'?: boolean | "false" | "true" | "mixed";
|
|
292
|
+
'aria-readonly'?: boolean | "false" | "true";
|
|
293
|
+
'aria-relevant'?: "all" | "text" | "additions" | "additions removals" | "additions text" | "removals" | "removals additions" | "removals text" | "text additions" | "text removals";
|
|
294
|
+
'aria-required'?: boolean | "false" | "true";
|
|
295
|
+
'aria-roledescription'?: string;
|
|
296
|
+
'aria-rowcount'?: number;
|
|
297
|
+
'aria-rowindex'?: number;
|
|
298
|
+
'aria-rowspan'?: number;
|
|
299
|
+
'aria-selected'?: boolean | "false" | "true";
|
|
300
|
+
'aria-setsize'?: number;
|
|
301
|
+
'aria-sort'?: "none" | "ascending" | "descending" | "other";
|
|
302
|
+
'aria-valuemax'?: number;
|
|
303
|
+
'aria-valuemin'?: number;
|
|
304
|
+
'aria-valuenow'?: number;
|
|
305
|
+
'aria-valuetext'?: string;
|
|
306
|
+
children?: React.ReactNode;
|
|
307
|
+
dangerouslySetInnerHTML?: {
|
|
308
|
+
__html: string;
|
|
309
|
+
};
|
|
310
|
+
onCopy?: React.ClipboardEventHandler<HTMLElement>;
|
|
311
|
+
onCopyCapture?: React.ClipboardEventHandler<HTMLElement>;
|
|
312
|
+
onCut?: React.ClipboardEventHandler<HTMLElement>;
|
|
313
|
+
onCutCapture?: React.ClipboardEventHandler<HTMLElement>;
|
|
314
|
+
onPaste?: React.ClipboardEventHandler<HTMLElement>;
|
|
315
|
+
onPasteCapture?: React.ClipboardEventHandler<HTMLElement>;
|
|
316
|
+
onCompositionEnd?: React.CompositionEventHandler<HTMLElement>;
|
|
317
|
+
onCompositionEndCapture?: React.CompositionEventHandler<HTMLElement>;
|
|
318
|
+
onCompositionStart?: React.CompositionEventHandler<HTMLElement>;
|
|
319
|
+
onCompositionStartCapture?: React.CompositionEventHandler<HTMLElement>;
|
|
320
|
+
onCompositionUpdate?: React.CompositionEventHandler<HTMLElement>;
|
|
321
|
+
onCompositionUpdateCapture?: React.CompositionEventHandler<HTMLElement>;
|
|
322
|
+
onFocus?: React.FocusEventHandler<HTMLElement>;
|
|
323
|
+
onFocusCapture?: React.FocusEventHandler<HTMLElement>;
|
|
324
|
+
onBlur?: React.FocusEventHandler<HTMLElement>;
|
|
325
|
+
onBlurCapture?: React.FocusEventHandler<HTMLElement>;
|
|
326
|
+
onChange?: React.FormEventHandler<HTMLElement>;
|
|
327
|
+
onChangeCapture?: React.FormEventHandler<HTMLElement>;
|
|
328
|
+
onBeforeInput?: React.FormEventHandler<HTMLElement>;
|
|
329
|
+
onBeforeInputCapture?: React.FormEventHandler<HTMLElement>;
|
|
330
|
+
onInput?: React.FormEventHandler<HTMLElement>;
|
|
331
|
+
onInputCapture?: React.FormEventHandler<HTMLElement>;
|
|
332
|
+
onReset?: React.FormEventHandler<HTMLElement>;
|
|
333
|
+
onResetCapture?: React.FormEventHandler<HTMLElement>;
|
|
334
|
+
onSubmit?: React.FormEventHandler<HTMLElement>;
|
|
335
|
+
onSubmitCapture?: React.FormEventHandler<HTMLElement>;
|
|
336
|
+
onInvalid?: React.FormEventHandler<HTMLElement>;
|
|
337
|
+
onInvalidCapture?: React.FormEventHandler<HTMLElement>;
|
|
338
|
+
onLoad?: React.ReactEventHandler<HTMLElement>;
|
|
339
|
+
onLoadCapture?: React.ReactEventHandler<HTMLElement>;
|
|
340
|
+
onError?: React.ReactEventHandler<HTMLElement>;
|
|
341
|
+
onErrorCapture?: React.ReactEventHandler<HTMLElement>;
|
|
342
|
+
onKeyDown?: React.KeyboardEventHandler<HTMLElement>;
|
|
343
|
+
onKeyDownCapture?: React.KeyboardEventHandler<HTMLElement>;
|
|
344
|
+
onKeyPress?: React.KeyboardEventHandler<HTMLElement>;
|
|
345
|
+
onKeyPressCapture?: React.KeyboardEventHandler<HTMLElement>;
|
|
346
|
+
onKeyUp?: React.KeyboardEventHandler<HTMLElement>;
|
|
347
|
+
onKeyUpCapture?: React.KeyboardEventHandler<HTMLElement>;
|
|
348
|
+
onAbort?: React.ReactEventHandler<HTMLElement>;
|
|
349
|
+
onAbortCapture?: React.ReactEventHandler<HTMLElement>;
|
|
350
|
+
onCanPlay?: React.ReactEventHandler<HTMLElement>;
|
|
351
|
+
onCanPlayCapture?: React.ReactEventHandler<HTMLElement>;
|
|
352
|
+
onCanPlayThrough?: React.ReactEventHandler<HTMLElement>;
|
|
353
|
+
onCanPlayThroughCapture?: React.ReactEventHandler<HTMLElement>;
|
|
354
|
+
onDurationChange?: React.ReactEventHandler<HTMLElement>;
|
|
355
|
+
onDurationChangeCapture?: React.ReactEventHandler<HTMLElement>;
|
|
356
|
+
onEmptied?: React.ReactEventHandler<HTMLElement>;
|
|
357
|
+
onEmptiedCapture?: React.ReactEventHandler<HTMLElement>;
|
|
358
|
+
onEncrypted?: React.ReactEventHandler<HTMLElement>;
|
|
359
|
+
onEncryptedCapture?: React.ReactEventHandler<HTMLElement>;
|
|
360
|
+
onEnded?: React.ReactEventHandler<HTMLElement>;
|
|
361
|
+
onEndedCapture?: React.ReactEventHandler<HTMLElement>;
|
|
362
|
+
onLoadedData?: React.ReactEventHandler<HTMLElement>;
|
|
363
|
+
onLoadedDataCapture?: React.ReactEventHandler<HTMLElement>;
|
|
364
|
+
onLoadedMetadata?: React.ReactEventHandler<HTMLElement>;
|
|
365
|
+
onLoadedMetadataCapture?: React.ReactEventHandler<HTMLElement>;
|
|
366
|
+
onLoadStart?: React.ReactEventHandler<HTMLElement>;
|
|
367
|
+
onLoadStartCapture?: React.ReactEventHandler<HTMLElement>;
|
|
368
|
+
onPause?: React.ReactEventHandler<HTMLElement>;
|
|
369
|
+
onPauseCapture?: React.ReactEventHandler<HTMLElement>;
|
|
370
|
+
onPlay?: React.ReactEventHandler<HTMLElement>;
|
|
371
|
+
onPlayCapture?: React.ReactEventHandler<HTMLElement>;
|
|
372
|
+
onPlaying?: React.ReactEventHandler<HTMLElement>;
|
|
373
|
+
onPlayingCapture?: React.ReactEventHandler<HTMLElement>;
|
|
374
|
+
onProgress?: React.ReactEventHandler<HTMLElement>;
|
|
375
|
+
onProgressCapture?: React.ReactEventHandler<HTMLElement>;
|
|
376
|
+
onRateChange?: React.ReactEventHandler<HTMLElement>;
|
|
377
|
+
onRateChangeCapture?: React.ReactEventHandler<HTMLElement>;
|
|
378
|
+
onSeeked?: React.ReactEventHandler<HTMLElement>;
|
|
379
|
+
onSeekedCapture?: React.ReactEventHandler<HTMLElement>;
|
|
380
|
+
onSeeking?: React.ReactEventHandler<HTMLElement>;
|
|
381
|
+
onSeekingCapture?: React.ReactEventHandler<HTMLElement>;
|
|
382
|
+
onStalled?: React.ReactEventHandler<HTMLElement>;
|
|
383
|
+
onStalledCapture?: React.ReactEventHandler<HTMLElement>;
|
|
384
|
+
onSuspend?: React.ReactEventHandler<HTMLElement>;
|
|
385
|
+
onSuspendCapture?: React.ReactEventHandler<HTMLElement>;
|
|
386
|
+
onTimeUpdate?: React.ReactEventHandler<HTMLElement>;
|
|
387
|
+
onTimeUpdateCapture?: React.ReactEventHandler<HTMLElement>;
|
|
388
|
+
onVolumeChange?: React.ReactEventHandler<HTMLElement>;
|
|
389
|
+
onVolumeChangeCapture?: React.ReactEventHandler<HTMLElement>;
|
|
390
|
+
onWaiting?: React.ReactEventHandler<HTMLElement>;
|
|
391
|
+
onWaitingCapture?: React.ReactEventHandler<HTMLElement>;
|
|
392
|
+
onAuxClick?: React.MouseEventHandler<HTMLElement>;
|
|
393
|
+
onAuxClickCapture?: React.MouseEventHandler<HTMLElement>;
|
|
394
|
+
onClick?: React.MouseEventHandler<HTMLElement>;
|
|
395
|
+
onClickCapture?: React.MouseEventHandler<HTMLElement>;
|
|
396
|
+
onContextMenu?: React.MouseEventHandler<HTMLElement>;
|
|
397
|
+
onContextMenuCapture?: React.MouseEventHandler<HTMLElement>;
|
|
398
|
+
onDoubleClick?: React.MouseEventHandler<HTMLElement>;
|
|
399
|
+
onDoubleClickCapture?: React.MouseEventHandler<HTMLElement>;
|
|
400
|
+
onDrag?: React.DragEventHandler<HTMLElement>;
|
|
401
|
+
onDragCapture?: React.DragEventHandler<HTMLElement>;
|
|
402
|
+
onDragEnd?: React.DragEventHandler<HTMLElement>;
|
|
403
|
+
onDragEndCapture?: React.DragEventHandler<HTMLElement>;
|
|
404
|
+
onDragEnter?: React.DragEventHandler<HTMLElement>;
|
|
405
|
+
onDragEnterCapture?: React.DragEventHandler<HTMLElement>;
|
|
406
|
+
onDragExit?: React.DragEventHandler<HTMLElement>;
|
|
407
|
+
onDragExitCapture?: React.DragEventHandler<HTMLElement>;
|
|
408
|
+
onDragLeave?: React.DragEventHandler<HTMLElement>;
|
|
409
|
+
onDragLeaveCapture?: React.DragEventHandler<HTMLElement>;
|
|
410
|
+
onDragOver?: React.DragEventHandler<HTMLElement>;
|
|
411
|
+
onDragOverCapture?: React.DragEventHandler<HTMLElement>;
|
|
412
|
+
onDragStart?: React.DragEventHandler<HTMLElement>;
|
|
413
|
+
onDragStartCapture?: React.DragEventHandler<HTMLElement>;
|
|
414
|
+
onDrop?: React.DragEventHandler<HTMLElement>;
|
|
415
|
+
onDropCapture?: React.DragEventHandler<HTMLElement>;
|
|
416
|
+
onMouseDown?: React.MouseEventHandler<HTMLElement>;
|
|
417
|
+
onMouseDownCapture?: React.MouseEventHandler<HTMLElement>;
|
|
418
|
+
onMouseEnter?: React.MouseEventHandler<HTMLElement>;
|
|
419
|
+
onMouseLeave?: React.MouseEventHandler<HTMLElement>;
|
|
420
|
+
onMouseMove?: React.MouseEventHandler<HTMLElement>;
|
|
421
|
+
onMouseMoveCapture?: React.MouseEventHandler<HTMLElement>;
|
|
422
|
+
onMouseOut?: React.MouseEventHandler<HTMLElement>;
|
|
423
|
+
onMouseOutCapture?: React.MouseEventHandler<HTMLElement>;
|
|
424
|
+
onMouseOver?: React.MouseEventHandler<HTMLElement>;
|
|
425
|
+
onMouseOverCapture?: React.MouseEventHandler<HTMLElement>;
|
|
426
|
+
onMouseUp?: React.MouseEventHandler<HTMLElement>;
|
|
427
|
+
onMouseUpCapture?: React.MouseEventHandler<HTMLElement>;
|
|
428
|
+
onSelect?: React.ReactEventHandler<HTMLElement>;
|
|
429
|
+
onSelectCapture?: React.ReactEventHandler<HTMLElement>;
|
|
430
|
+
onTouchCancel?: React.TouchEventHandler<HTMLElement>;
|
|
431
|
+
onTouchCancelCapture?: React.TouchEventHandler<HTMLElement>;
|
|
432
|
+
onTouchEnd?: React.TouchEventHandler<HTMLElement>;
|
|
433
|
+
onTouchEndCapture?: React.TouchEventHandler<HTMLElement>;
|
|
434
|
+
onTouchMove?: React.TouchEventHandler<HTMLElement>;
|
|
435
|
+
onTouchMoveCapture?: React.TouchEventHandler<HTMLElement>;
|
|
436
|
+
onTouchStart?: React.TouchEventHandler<HTMLElement>;
|
|
437
|
+
onTouchStartCapture?: React.TouchEventHandler<HTMLElement>;
|
|
438
|
+
onPointerDown?: React.PointerEventHandler<HTMLElement>;
|
|
439
|
+
onPointerDownCapture?: React.PointerEventHandler<HTMLElement>;
|
|
440
|
+
onPointerMove?: React.PointerEventHandler<HTMLElement>;
|
|
441
|
+
onPointerMoveCapture?: React.PointerEventHandler<HTMLElement>;
|
|
442
|
+
onPointerUp?: React.PointerEventHandler<HTMLElement>;
|
|
443
|
+
onPointerUpCapture?: React.PointerEventHandler<HTMLElement>;
|
|
444
|
+
onPointerCancel?: React.PointerEventHandler<HTMLElement>;
|
|
445
|
+
onPointerCancelCapture?: React.PointerEventHandler<HTMLElement>;
|
|
446
|
+
onPointerEnter?: React.PointerEventHandler<HTMLElement>;
|
|
447
|
+
onPointerEnterCapture?: React.PointerEventHandler<HTMLElement>;
|
|
448
|
+
onPointerLeave?: React.PointerEventHandler<HTMLElement>;
|
|
449
|
+
onPointerLeaveCapture?: React.PointerEventHandler<HTMLElement>;
|
|
450
|
+
onPointerOver?: React.PointerEventHandler<HTMLElement>;
|
|
451
|
+
onPointerOverCapture?: React.PointerEventHandler<HTMLElement>;
|
|
452
|
+
onPointerOut?: React.PointerEventHandler<HTMLElement>;
|
|
453
|
+
onPointerOutCapture?: React.PointerEventHandler<HTMLElement>;
|
|
454
|
+
onGotPointerCapture?: React.PointerEventHandler<HTMLElement>;
|
|
455
|
+
onGotPointerCaptureCapture?: React.PointerEventHandler<HTMLElement>;
|
|
456
|
+
onLostPointerCapture?: React.PointerEventHandler<HTMLElement>;
|
|
457
|
+
onLostPointerCaptureCapture?: React.PointerEventHandler<HTMLElement>;
|
|
458
|
+
onScroll?: React.UIEventHandler<HTMLElement>;
|
|
459
|
+
onScrollCapture?: React.UIEventHandler<HTMLElement>;
|
|
460
|
+
onWheel?: React.WheelEventHandler<HTMLElement>;
|
|
461
|
+
onWheelCapture?: React.WheelEventHandler<HTMLElement>;
|
|
462
|
+
onAnimationStart?: React.AnimationEventHandler<HTMLElement>;
|
|
463
|
+
onAnimationStartCapture?: React.AnimationEventHandler<HTMLElement>;
|
|
464
|
+
onAnimationEnd?: React.AnimationEventHandler<HTMLElement>;
|
|
465
|
+
onAnimationEndCapture?: React.AnimationEventHandler<HTMLElement>;
|
|
466
|
+
onAnimationIteration?: React.AnimationEventHandler<HTMLElement>;
|
|
467
|
+
onAnimationIterationCapture?: React.AnimationEventHandler<HTMLElement>;
|
|
468
|
+
onTransitionEnd?: React.TransitionEventHandler<HTMLElement>;
|
|
469
|
+
onTransitionEndCapture?: React.TransitionEventHandler<HTMLElement>;
|
|
470
|
+
css?: _emotion_core.InterpolationWithTheme<any>;
|
|
471
|
+
ref?: React.LegacyRef<HTMLElement>;
|
|
472
|
+
key?: React.Key;
|
|
473
|
+
}, HTMLElement>;
|
|
474
|
+
|
|
475
|
+
declare type CompoundPopover = typeof Popover$1 & {
|
|
476
|
+
Content: typeof PopoverContent;
|
|
477
|
+
Trigger: typeof PopoverTrigger;
|
|
478
|
+
};
|
|
479
|
+
declare const Popover: CompoundPopover;
|
|
480
|
+
|
|
481
|
+
export { Popover, PopoverContent, PopoverContentProps, PopoverProps, PopoverTrigger, PopoverTriggerProps };
|
package/dist/index.js
CHANGED
|
@@ -1,22 +1,21 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var O = require('react');
|
|
3
|
+
var L = require('react');
|
|
6
4
|
var f36Core = require('@contentful/f36-core');
|
|
7
5
|
var reactPopper = require('react-popper');
|
|
8
6
|
var emotion = require('emotion');
|
|
9
7
|
var f36Utils = require('@contentful/f36-utils');
|
|
10
|
-
var
|
|
8
|
+
var x = require('@contentful/f36-tokens');
|
|
11
9
|
|
|
12
|
-
function
|
|
10
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
13
11
|
|
|
14
|
-
var
|
|
15
|
-
var
|
|
12
|
+
var L__default = /*#__PURE__*/_interopDefault(L);
|
|
13
|
+
var x__default = /*#__PURE__*/_interopDefault(x);
|
|
16
14
|
|
|
17
|
-
var
|
|
15
|
+
var te=Object.defineProperty,re=Object.defineProperties;var ne=Object.getOwnPropertyDescriptors;var y=Object.getOwnPropertySymbols;var k=Object.prototype.hasOwnProperty,A=Object.prototype.propertyIsEnumerable;var F=(e,t,o)=>t in e?te(e,t,{enumerable:!0,configurable:!0,writable:!0,value:o}):e[t]=o,a=(e,t)=>{for(var o in t||(t={}))k.call(t,o)&&F(e,o,t[o]);if(y)for(var o of y(t))A.call(t,o)&&F(e,o,t[o]);return e},m=(e,t)=>re(e,ne(t));var N=(e,t)=>{var o={};for(var r in e)k.call(e,r)&&t.indexOf(r)<0&&(o[r]=e[r]);if(e!=null&&y)for(var r of y(e))t.indexOf(r)<0&&A.call(e,r)&&(o[r]=e[r]);return o};var K=(e,t,o)=>new Promise((r,f)=>{var v=s=>{try{l(o.next(s));}catch(n){f(n);}},P=s=>{try{l(o.throw(s));}catch(n){f(n);}},l=s=>s.done?r(s.value):Promise.resolve(s.value).then(v,P);l((o=o.apply(e,t)).next());});var U=L__default.default.createContext(void 0),h=()=>{let e=L__default.default.useContext(U);if(e===void 0)throw new Error("usePopoverContext must be used within a PopoverContextProvider");return e},$=U.Provider;function V(e){let{children:t,isOpen:o,placement:r="bottom-start",isFullWidth:f=!1,isAutoalignmentEnabled:v=!0,usePortal:P=!0,closeOnBlur:l=!0,closeOnEsc:s=!0,onClose:n,autoFocus:C=!0,id:E,offset:T=[1,4],renderOnlyWhenOpen:g=!0}=e,[i,X]=L.useState(null),[c,Y]=L.useState(null),{attributes:S,update:M,styles:I}=reactPopper.usePopper(i,c,{placement:r,modifiers:[{name:"offset",options:{offset:T}},m(a({},ue),{enabled:f}),{name:"preventOverflow",enabled:v,options:{mainAxis:!0}},{name:"flip",enabled:v}]});L.useEffect(()=>{o&&C&&c&&c.focus({preventScroll:!0});},[o,c]),L.useEffect(()=>{(()=>K(this,null,function*(){o&&M&&(yield M());}))();},[o,M]);let Z=f36Core.useId(void 0,"popover-content"),w=E||Z,B=L.useCallback(()=>{n==null||n(),setTimeout(()=>i==null?void 0:i.focus({preventScroll:!0}),0);},[n,i]),O=L.useRef(!1),_=L.useMemo(()=>({isOpen:!!o,usePortal:P,renderOnlyWhenOpen:g,getTriggerProps:(p={},H=null)=>({onMouseDown:u=>{var d;O.current=!0,(d=p.onMouseDown)==null||d.call(p,u);},onMouseUp:u=>{var d;O.current=!1,(d=p.onMouseUp)==null||d.call(p,u);},ref:f36Core.mergeRefs(X,H),"aria-expanded":!!o,"aria-controls":w}),getPopoverProps:(p={},H=null)=>m(a({},S.popper),{style:a(a({},p.style||{}),I.popper),ref:f36Core.mergeRefs(Y,H),id:w,onBlur:u=>{if(p.onBlur&&p.onBlur(u),!l)return;let d=document.activeElement,b=u.relatedTarget||d,ee=c===b||(c==null?void 0:c.contains(b)),oe=i===b||(i==null?void 0:i.contains(b))||O.current;ee||oe||n==null||n();},onKeyDown:u=>{p.onKeyDown&&p.onKeyDown(u),s&&u.key==="Escape"&&B();}})}),[o,g,S,I,P,w,s,l,c,i,B,n]);return L__default.default.createElement($,{value:_},t)}var ue={name:"sameWidth",enabled:!0,phase:"beforeWrite",requires:["computeStyles"],fn:({state:e})=>{e.styles.popper.width=`${e.rects.reference.width}px`;},effect:({state:e})=>()=>{let t=e.elements.reference;e.elements.popper.style.width=`${t.offsetWidth}px`;}};var J=e=>({container:emotion.css({display:e?"initial":"none",background:x__default.default.colorWhite,border:0,borderRadius:x__default.default.borderRadiusMedium,boxShadow:x__default.default.boxShadowDefault,zIndex:x__default.default.zIndexDropdown,"&:focus":{boxShadow:x__default.default.glowPrimary,outline:"none"},"&:focus:not(:focus-visible)":{boxShadow:x__default.default.boxShadowDefault}})});var fe=(e,t)=>{let g=e,{children:o,className:r,testId:f="cf-ui-popover-content",role:v="dialog"}=g,P=N(g,["children","className","testId","role"]),{isOpen:l,renderOnlyWhenOpen:s,getPopoverProps:n,usePortal:C}=h(),E=J(l),T=L__default.default.createElement("div",m(a(a({},P),n(P,t)),{className:emotion.cx(E.container,r),"data-test-id":f,tabIndex:-1,role:v,"data-position-absolute":!0}),o);return s&&!l?null:C?L__default.default.createElement(f36Utils.Portal,null,T):T},R=L__default.default.forwardRef(fe);var D=e=>{var r;let t=L__default.default.Children.only(e.children),{getTriggerProps:o}=h();return L__default.default.cloneElement(t,m(a({},o(t.props,t.ref)),{"aria-haspopup":(r=t.props["aria-haspopup"])!=null?r:"dialog"}))};var W=V;W.Content=R;W.Trigger=D;
|
|
18
16
|
|
|
19
|
-
exports.Popover =
|
|
20
|
-
exports.PopoverContent =
|
|
21
|
-
exports.PopoverTrigger =
|
|
17
|
+
exports.Popover = W;
|
|
18
|
+
exports.PopoverContent = R;
|
|
19
|
+
exports.PopoverTrigger = D;
|
|
20
|
+
//# sourceMappingURL=out.js.map
|
|
22
21
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/Popover.tsx","../src/PopoverContext.ts","../src/PopoverContent/PopoverContent.tsx","../src/PopoverContent/PopoverContent.styles.ts","../src/PopoverTrigger/PopoverTrigger.tsx","../src/CompoundPopover.tsx"],"names":["React","useMemo","useState","useEffect","useCallback","useId","mergeRefs","PopoverContext","usePopoverContext","context","PopoverContextProvider","usePopper","Popover","props","children","isOpen","placement","isFullWidth","isAutoalignmentEnabled","usePortal","closeOnBlur","closeOnEsc","onClose","autoFocus","id","offset","renderOnlyWhenOpen","triggerElement","setTriggerElement","popoverElement","setPopoverElement","popperAttributes","update","popperStyles","__spreadProps","__spreadValues","sameWidth","__async","popoverGeneratedId","popoverId","closeAndFocusTrigger","contextValue","_ref","_props","event","relatedTarget","targetIsPopover","targetIsTrigger","state","reference","cx","Portal","css","tokens","getPopoverContentStyles","_PopoverContent","ref","_a","className","testId","role","otherProps","__objRest","getPopoverProps","styles","content","PopoverContent","PopoverTrigger","child","getTriggerProps"],"mappings":"kyBAAA,OAAOA,IAAS,WAAAC,GAAS,YAAAC,EAAU,aAAAC,EAAW,eAAAC,OAAmB,QACjE,OAAS,SAAAC,GAAO,aAAAC,MAAmC,uBCDnD,OAAON,MAA0B,QAajC,IAAMO,EAAiBP,EAAM,cAC3B,MACF,EAEaQ,EAAoB,IAAM,CACrC,IAAMC,EAAUT,EAAM,WAAWO,CAAc,EAE/C,GAAIE,IAAY,OACd,MAAM,IAAI,MACR,gEACF,EAGF,OAAOA,CACT,EAEaC,EAAyBH,EAAe,SDzBrD,OAAS,aAAAI,OAAiB,eAuGnB,SAASC,EAAQC,EAAkC,CACxD,GAAM,CACJ,SAAAC,EACA,OAAAC,EACA,UAAAC,EAAY,eACZ,YAAAC,EAAc,GACd,uBAAAC,EAAyB,GACzB,UAAAC,EAAY,GACZ,YAAAC,EAAc,GACd,WAAAC,EAAa,GACb,QAAAC,EACA,UAAAC,EAAY,GACZ,GAAAC,EACA,OAAAC,EAAS,CAAC,EAAG,CAAC,EACd,mBAAAC,EAAqB,EACvB,EAAIb,EAEE,CAACc,EAAgBC,CAAiB,EAAI1B,EAC1C,IACF,EACM,CAAC2B,EAAgBC,CAAiB,EAAI5B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+B,CAC7B,UAAWA,EAAO,gBACpB,CACF,CAAC,CACH,GDCA,IAAME,GAAkB,CAAC1C,EAAyC2C,IAAQ,CACxE,IAMIC,EAAA5C,EALF,UAAAC,EACA,UAAA4C,EACA,OAAAC,EAAS,wBACT,KAAAC,EAAO,QAzBX,EA2BMH,EADCI,EAAAC,EACDL,EADC,CAJH,WACA,YACA,SACA,SAGI,CAAE,OAAA1C,EAAQ,mBAAAW,EAAoB,gBAAAqC,EAAiB,UAAA5C,CAAU,EAC7DX,EAAkB,EAEdwD,EAASV,EAAwBvC,CAAM,EAEvCkD,EACJjE,EAAA,cAAC,MAAAkC,EAAAC,IAAA,GACK0B,GACAE,EAAgBF,EAAYL,CAAG,GAFpC,CAGC,UAAWN,GAAGc,EAAO,UAAWN,CAAS,EACzC,eAAcC,EACd,SAAU,GACV,KAAMC,EAGN,yBAAsB,KAErB9C,CACH,EAGF,OAAIY,GAAsB,CAACX,EAClB,KAGFI,EAAYnB,EAAA,cAACmD,GAAA,KAAQc,CAAQ,EAAYA,CAClD,EAEaC,EAAiBlE,EAAM,WAAWuD,EAAe,EExD9D,OAAOvD,MAAW,QAUX,IAAMmE,EAAkBtD,GAA+B,CAV9D,IAAA4C,EAWE,IAAMW,EAAQpE,EAAM,SAAS,KAAKa,EAAM,QAAQ,EAC1C,CAAE,gBAAAwD,CAAgB,EAAI7D,EAAkB,EAE9C,OAAOR,EAAM,aAAaoE,EAAOlC,EAAAC,EAAA,GAC5BkC,EAAgBD,EAAM,GAAG,GADG,CAE/B,iBAAiBX,EAAAW,EAAM,MAAM,mBAAZ,KAAAX,EAAgC,QACnD,EAAC,CACH,ECTO,IAAM7C,EAAUA,EACvBA,EAAQ,QAAUsD,EAClBtD,EAAQ,QAAUuD","sourcesContent":["import React, { useMemo, useState, useEffect, useCallback } from 'react';\nimport { useId, mergeRefs, type ExpandProps } from '@contentful/f36-core';\nimport type { Placement, Modifier } from '@popperjs/core';\nimport { PopoverContextProvider, PopoverContextType } from './PopoverContext';\nimport { usePopper } from 'react-popper';\n\nexport interface PopoverProps {\n children: React.ReactNode;\n\n /**\n * Boolean to determine if the Popover should be the same width as\n * the trigger element\n *\n * @default false\n */\n isFullWidth?: boolean;\n\n /**\n * Boolean to control whether or not the Popover is open\n *\n * @default false\n */\n isOpen?: boolean;\n\n /**\n * Callback fired when the popover closes\n */\n onClose?: () => void;\n\n /**\n * Determines the preferred position of the Popover. This position is not\n * guaranteed, as the Popover might be moved to fit the viewport\n *\n * @default bottom-start\n */\n placement?: Placement;\n\n /**\n * Boolean to control if popover is allowed to change its placement automatically\n * based on available space in the viewport.\n *\n * For example:\n * If you set placement prop to bottom, but there isn't enough space to position the popover in that direction,\n * it will change the popper placement to top. As soon as enough space is detected, the placement will be reverted to the defined one.\n *\n * If you want the popover to strictly follow the placement prop you should set this prop to false.\n *\n * @default true\n */\n isAutoalignmentEnabled?: boolean;\n\n /**\n * Boolean to control whether or not to render the Popover in a React Portal.\n * Rendering content inside a Portal allows the Popover to escape the bounds\n * of its parent while still being positioned correctly. Using a Portal is\n * necessary if an ancestor of the Popover hides overflow.\n *\n * @default true\n */\n usePortal?: boolean;\n\n /**\n * If true, the popover will close when you blur out it by clicking outside or tabbing out\n *\n * @default true\n */\n closeOnBlur?: boolean;\n\n /**\n * If true, the popover will close when you hit the Esc key\n *\n * @default true\n */\n closeOnEsc?: boolean;\n\n /**\n * If true, the popover will be focused after opening\n *\n * @default true\n */\n autoFocus?: boolean;\n\n /**\n * Popover id. Will be used as an `id` attribute on popover\n * and as `aria-controls` attribute on trigger\n *\n * @default true\n */\n id?: string;\n\n /**\n * The `X-axis` and `Y-axis` offset to position popper element\n * from its trigger element. `[X, Y]`\n *\n * @default [1, 4]\n */\n offset?: [number, number];\n\n /**\n * Defines if popover should be rendered in the DOM only when it's open\n * or all the time (after the component has been mounted)\n *\n * @default true\n */\n renderOnlyWhenOpen?: boolean;\n}\n\nexport function Popover(props: ExpandProps<PopoverProps>) {\n const {\n children,\n isOpen,\n placement = 'bottom-start',\n isFullWidth = false,\n isAutoalignmentEnabled = true,\n usePortal = true,\n closeOnBlur = true,\n closeOnEsc = true,\n onClose,\n autoFocus = true,\n id,\n offset = [1, 4],\n renderOnlyWhenOpen = true,\n } = props;\n\n const [triggerElement, setTriggerElement] = useState<HTMLElement | null>(\n null,\n );\n const [popoverElement, setPopoverElement] = useState<HTMLElement | null>(\n null,\n );\n\n const {\n attributes: popperAttributes,\n update,\n styles: popperStyles,\n } = usePopper(triggerElement, popoverElement, {\n placement,\n modifiers: [\n {\n name: 'offset',\n options: {\n offset,\n },\n },\n {\n ...sameWidth,\n enabled: isFullWidth,\n },\n {\n name: 'preventOverflow',\n enabled: isAutoalignmentEnabled,\n options: {\n mainAxis: true,\n },\n },\n {\n name: 'flip',\n enabled: isAutoalignmentEnabled,\n },\n ],\n });\n\n useEffect(() => {\n if (isOpen && autoFocus && popoverElement) {\n popoverElement.focus({ preventScroll: true });\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [isOpen, popoverElement]);\n\n useEffect(() => {\n const updatePosition = async () => {\n if (isOpen && update) {\n await update();\n }\n };\n updatePosition();\n }, [isOpen, update]);\n\n const popoverGeneratedId = useId(undefined, 'popover-content');\n const popoverId = id || popoverGeneratedId;\n\n const closeAndFocusTrigger = useCallback(() => {\n onClose?.();\n\n // setTimeout trick to make it work with focus-lock\n setTimeout(() => triggerElement?.focus({ preventScroll: true }), 0);\n }, [onClose, triggerElement]);\n\n const contextValue: PopoverContextType = useMemo(\n () => ({\n isOpen: Boolean(isOpen),\n usePortal,\n renderOnlyWhenOpen,\n getTriggerProps: (_ref = null) => ({\n ref: mergeRefs(setTriggerElement, _ref),\n ['aria-expanded']: Boolean(isOpen),\n ['aria-controls']: popoverId,\n }),\n getPopoverProps: (_props = {}, _ref = null) => ({\n ...popperAttributes.popper,\n style: {\n ...(_props.style || {}),\n ...popperStyles.popper,\n },\n ref: mergeRefs(setPopoverElement, _ref),\n id: popoverId,\n onBlur: (event: React.FocusEvent<HTMLDivElement>) => {\n if (_props.onBlur) {\n _props.onBlur(event);\n }\n\n if (!closeOnBlur) {\n return;\n }\n\n const relatedTarget = event.relatedTarget as Node;\n\n const targetIsPopover =\n popoverElement === relatedTarget ||\n popoverElement?.contains(relatedTarget);\n const targetIsTrigger =\n triggerElement === relatedTarget ||\n triggerElement?.contains(relatedTarget);\n\n if (targetIsPopover || targetIsTrigger) {\n return;\n }\n\n onClose?.();\n },\n onKeyDown: (event: React.KeyboardEvent<HTMLDivElement>) => {\n if (_props.onKeyDown) {\n _props.onKeyDown(event);\n }\n\n if (closeOnEsc && event.key === 'Escape') {\n closeAndFocusTrigger();\n }\n },\n }),\n }),\n [\n isOpen,\n renderOnlyWhenOpen,\n popperAttributes,\n popperStyles,\n usePortal,\n popoverId,\n closeOnEsc,\n closeOnBlur,\n popoverElement,\n triggerElement,\n closeAndFocusTrigger,\n onClose,\n ],\n );\n\n return (\n <PopoverContextProvider value={contextValue}>\n {children}\n </PopoverContextProvider>\n );\n}\n\n/**\n * Sets the popover width to the size of the trigger element.\n */\nconst sameWidth: Modifier<'sameWidth', any> = {\n name: 'sameWidth',\n enabled: true,\n phase: 'beforeWrite',\n requires: ['computeStyles'],\n fn: ({ state }) => {\n state.styles.popper.width = `${state.rects.reference.width}px`;\n },\n effect:\n ({ state }) =>\n () => {\n const reference = state.elements.reference as HTMLElement;\n state.elements.popper.style.width = `${reference.offsetWidth}px`;\n },\n};\n","import React, { HTMLProps } from 'react';\n\nexport type PopoverContextType = {\n isOpen: boolean;\n usePortal: boolean;\n renderOnlyWhenOpen: boolean;\n getPopoverProps: (\n _props: HTMLProps<HTMLDivElement>,\n _ref: React.Ref<HTMLDivElement>,\n ) => HTMLProps<HTMLDivElement>;\n getTriggerProps: (_ref: React.Ref<HTMLElement>) => HTMLProps<HTMLElement>;\n};\n\nconst PopoverContext = React.createContext<PopoverContextType | undefined>(\n undefined,\n);\n\nexport const usePopoverContext = () => {\n const context = React.useContext(PopoverContext);\n\n if (context === undefined) {\n throw new Error(\n 'usePopoverContext must be used within a PopoverContextProvider',\n );\n }\n\n return context;\n};\n\nexport const PopoverContextProvider = PopoverContext.Provider;\n","import React from 'react';\nimport { cx } from 'emotion';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\nimport { usePopoverContext } from '../PopoverContext';\nimport { Portal } from '@contentful/f36-utils';\nimport { getPopoverContentStyles } from './PopoverContent.styles';\n\ninterface PopoverContentInternalProps extends CommonProps {\n children?: React.ReactNode;\n}\n\nexport type PopoverContentProps = PropsWithHTMLElement<\n PopoverContentInternalProps,\n 'div'\n>;\n\nconst _PopoverContent = (props: ExpandProps<PopoverContentProps>, ref) => {\n const {\n children,\n className,\n testId = 'cf-ui-popover-content',\n role = 'dialog',\n ...otherProps\n } = props;\n const { isOpen, renderOnlyWhenOpen, getPopoverProps, usePortal } =\n usePopoverContext();\n\n const styles = getPopoverContentStyles(isOpen);\n\n const content = (\n <div\n {...otherProps}\n {...getPopoverProps(otherProps, ref)}\n className={cx(styles.container, className)}\n data-test-id={testId}\n tabIndex={-1}\n role={role}\n // specific attribute to mark that this element is absolute positioned\n // for internal contentful apps usage\n data-position-absolute\n >\n {children}\n </div>\n );\n\n if (renderOnlyWhenOpen && !isOpen) {\n return null;\n }\n\n return usePortal ? <Portal>{content}</Portal> : content;\n};\n\nexport const PopoverContent = React.forwardRef(_PopoverContent);\n","import { css } from 'emotion';\nimport tokens from '@contentful/f36-tokens';\n\nexport const getPopoverContentStyles = (isOpen: boolean) => ({\n container: css({\n display: isOpen ? 'initial' : 'none',\n background: tokens.colorWhite,\n border: 0,\n borderRadius: tokens.borderRadiusMedium,\n boxShadow: tokens.boxShadowDefault,\n zIndex: tokens.zIndexDropdown,\n '&:focus': {\n boxShadow: tokens.glowPrimary,\n outline: 'none',\n },\n '&:focus:not(:focus-visible)': {\n boxShadow: tokens.boxShadowDefault,\n },\n }),\n});\n","import React from 'react';\nimport { usePopoverContext } from '../PopoverContext';\n\nexport interface PopoverTriggerProps {\n children: React.ReactNode;\n}\n\n/**\n * PopoverTrigger opens the popover. It must be an interactive element.\n */\nexport const PopoverTrigger = (props: PopoverTriggerProps) => {\n const child = React.Children.only(props.children) as any;\n const { getTriggerProps } = usePopoverContext();\n\n return React.cloneElement(child, {\n ...getTriggerProps(child.ref),\n 'aria-haspopup': child.props['aria-haspopup'] ?? 'dialog',\n });\n};\n","import { Popover as OriginalPopover } from './Popover';\nimport { PopoverContent } from './PopoverContent/PopoverContent';\nimport { PopoverTrigger } from './PopoverTrigger/PopoverTrigger';\n\ntype CompoundPopover = typeof OriginalPopover & {\n Content: typeof PopoverContent;\n Trigger: typeof PopoverTrigger;\n};\n\nexport const Popover = OriginalPopover as CompoundPopover;\nPopover.Content = PopoverContent;\nPopover.Trigger = PopoverTrigger;\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/Popover.tsx","../src/PopoverContext.ts","../src/PopoverContent/PopoverContent.tsx","../src/PopoverContent/PopoverContent.styles.ts","../src/PopoverTrigger/PopoverTrigger.tsx","../src/CompoundPopover.tsx"],"names":["React","useMemo","useState","useEffect","useCallback","useRef","useId","mergeRefs","PopoverContext","usePopoverContext","context","PopoverContextProvider","usePopper","Popover","props","children","isOpen","placement","isFullWidth","isAutoalignmentEnabled","usePortal","closeOnBlur","closeOnEsc","onClose","autoFocus","id","offset","renderOnlyWhenOpen","triggerElement","setTriggerElement","popoverElement","setPopoverElement","popperAttributes","update","popperStyles","__spreadProps","__spreadValues","sameWidth","__async","popoverGeneratedId","popoverId","closeAndFocusTrigger","isMouseDown","contextValue","_props","_ref","event","_a","activeElement","relatedTarget","targetIsPopover","targetIsTrigger","state","reference","cx","Portal","css","tokens","getPopoverContentStyles","_PopoverContent","ref","className","testId","role","otherProps","__objRest","getPopoverProps","styles","content","PopoverContent","PopoverTrigger","child","getTriggerProps"],"mappings":"kyBAAA,OAAOA,IACL,WAAAC,GACA,YAAAC,EACA,aAAAC,EACA,eAAAC,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ACA,SAGI,CAAE,OAAA/B,EAAQ,mBAAAW,EAAoB,gBAAAuC,EAAiB,UAAA9C,CAAU,EAC7DX,EAAkB,EAEd0D,EAAST,EAAwB1C,CAAM,EAEvCoD,EACJpE,EAAA,cAAC,MAAAmC,EAAAC,IAAA,GACK4B,GACAE,EAAgBF,EAAYJ,CAAG,GAFpC,CAGC,UAAWN,GAAGa,EAAO,UAAWN,CAAS,EACzC,eAAcC,EACd,SAAU,GACV,KAAMC,EAGN,yBAAsB,KAErBhD,CACH,EAGF,OAAIY,GAAsB,CAACX,EAClB,KAGFI,EAAYpB,EAAA,cAACuD,GAAA,KAAQa,CAAQ,EAAYA,CAClD,EAEaC,EAAiBrE,EAAM,WAAW2D,EAAe,EExD9D,OAAO3D,MAAW,QAUX,IAAMsE,EAAkBxD,GAA+B,CAV9D,IAAAiC,EAWE,IAAMwB,EAAQvE,EAAM,SAAS,KAAKc,EAAM,QAAQ,EAC1C,CAAE,gBAAA0D,CAAgB,EAAI/D,EAAkB,EAE9C,OAAOT,EAAM,aAAauE,EAAOpC,EAAAC,EAAA,GAC5BoC,EAAgBD,EAAM,MAAOA,EAAM,GAAG,GADV,CAE/B,iBAAiBxB,EAAAwB,EAAM,MAAM,eAAe,IAA3B,KAAAxB,EAAgC,QACnD,EAAC,CACH,ECTO,IAAMlC,EAAUA,EACvBA,EAAQ,QAAUwD,EAClBxD,EAAQ,QAAUyD","sourcesContent":["import React, {\n useMemo,\n useState,\n useEffect,\n useCallback,\n useRef,\n} from 'react';\nimport { useId, mergeRefs, type ExpandProps } from '@contentful/f36-core';\nimport type { Placement, Modifier } from '@popperjs/core';\nimport { PopoverContextProvider, PopoverContextType } from './PopoverContext';\nimport { usePopper } from 'react-popper';\n\nexport interface PopoverProps {\n children: React.ReactNode;\n\n /**\n * Boolean to determine if the Popover should be the same width as\n * the trigger element\n *\n * @default false\n */\n isFullWidth?: boolean;\n\n /**\n * Boolean to control whether or not the Popover is open\n *\n * @default false\n */\n isOpen?: boolean;\n\n /**\n * Callback fired when the popover closes\n */\n onClose?: () => void;\n\n /**\n * Determines the preferred position of the Popover. This position is not\n * guaranteed, as the Popover might be moved to fit the viewport\n *\n * @default bottom-start\n */\n placement?: Placement;\n\n /**\n * Boolean to control if popover is allowed to change its placement automatically\n * based on available space in the viewport.\n *\n * For example:\n * If you set placement prop to bottom, but there isn't enough space to position the popover in that direction,\n * it will change the popper placement to top. As soon as enough space is detected, the placement will be reverted to the defined one.\n *\n * If you want the popover to strictly follow the placement prop you should set this prop to false.\n *\n * @default true\n */\n isAutoalignmentEnabled?: boolean;\n\n /**\n * Boolean to control whether or not to render the Popover in a React Portal.\n * Rendering content inside a Portal allows the Popover to escape the bounds\n * of its parent while still being positioned correctly. Using a Portal is\n * necessary if an ancestor of the Popover hides overflow.\n *\n * @default true\n */\n usePortal?: boolean;\n\n /**\n * If true, the popover will close when you blur out it by clicking outside or tabbing out\n *\n * @default true\n */\n closeOnBlur?: boolean;\n\n /**\n * If true, the popover will close when you hit the Esc key\n *\n * @default true\n */\n closeOnEsc?: boolean;\n\n /**\n * If true, the popover will be focused after opening\n *\n * @default true\n */\n autoFocus?: boolean;\n\n /**\n * Popover id. Will be used as an `id` attribute on popover\n * and as `aria-controls` attribute on trigger\n *\n * @default true\n */\n id?: string;\n\n /**\n * The `X-axis` and `Y-axis` offset to position popper element\n * from its trigger element. `[X, Y]`\n *\n * @default [1, 4]\n */\n offset?: [number, number];\n\n /**\n * Defines if popover should be rendered in the DOM only when it's open\n * or all the time (after the component has been mounted)\n *\n * @default true\n */\n renderOnlyWhenOpen?: boolean;\n}\n\nexport function Popover(props: ExpandProps<PopoverProps>) {\n const {\n children,\n isOpen,\n placement = 'bottom-start',\n isFullWidth = false,\n isAutoalignmentEnabled = true,\n usePortal = true,\n closeOnBlur = true,\n closeOnEsc = true,\n onClose,\n autoFocus = true,\n id,\n offset = [1, 4],\n renderOnlyWhenOpen = true,\n } = props;\n\n const [triggerElement, setTriggerElement] = useState<HTMLElement | null>(\n null,\n );\n const [popoverElement, setPopoverElement] = useState<HTMLElement | null>(\n null,\n );\n\n const {\n attributes: popperAttributes,\n update,\n styles: popperStyles,\n } = usePopper(triggerElement, popoverElement, {\n placement,\n modifiers: [\n {\n name: 'offset',\n options: {\n offset,\n },\n },\n {\n ...sameWidth,\n enabled: isFullWidth,\n },\n {\n name: 'preventOverflow',\n enabled: isAutoalignmentEnabled,\n options: {\n mainAxis: true,\n },\n },\n {\n name: 'flip',\n enabled: isAutoalignmentEnabled,\n },\n ],\n });\n\n useEffect(() => {\n if (isOpen && autoFocus && popoverElement) {\n popoverElement.focus({ preventScroll: true });\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [isOpen, popoverElement]);\n\n useEffect(() => {\n const updatePosition = async () => {\n if (isOpen && update) {\n await update();\n }\n };\n updatePosition();\n }, [isOpen, update]);\n\n const popoverGeneratedId = useId(undefined, 'popover-content');\n const popoverId = id || popoverGeneratedId;\n\n const closeAndFocusTrigger = useCallback(() => {\n onClose?.();\n\n // setTimeout trick to make it work with focus-lock\n setTimeout(() => triggerElement?.focus({ preventScroll: true }), 0);\n }, [onClose, triggerElement]);\n\n // Safari has an issue with the relatedTarget that we use on the onBlur for getPopoverProps,\n // which was causing the popover to close and reopen when clicking on the trigger.\n // We will use the isMouseDown to prevent triggering blur in the cases where the user clicks on the trigger.\n const isMouseDown = useRef<Boolean>(false);\n\n const contextValue: PopoverContextType = useMemo(\n () => ({\n isOpen: Boolean(isOpen),\n usePortal,\n renderOnlyWhenOpen,\n getTriggerProps: (_props = {}, _ref = null) => ({\n onMouseDown: (event) => {\n isMouseDown.current = true;\n _props.onMouseDown?.(event);\n },\n onMouseUp: (event) => {\n isMouseDown.current = false;\n _props.onMouseUp?.(event);\n },\n ref: mergeRefs(setTriggerElement, _ref),\n ['aria-expanded']: Boolean(isOpen),\n ['aria-controls']: popoverId,\n }),\n getPopoverProps: (_props = {}, _ref = null) => ({\n ...popperAttributes.popper,\n style: {\n ...(_props.style || {}),\n ...popperStyles.popper,\n },\n ref: mergeRefs(setPopoverElement, _ref),\n id: popoverId,\n onBlur: (event: React.FocusEvent<HTMLDivElement>) => {\n if (_props.onBlur) {\n _props.onBlur(event);\n }\n\n if (!closeOnBlur) {\n return;\n }\n\n const activeElement = document.activeElement;\n const relatedTarget = event.relatedTarget || activeElement;\n\n const targetIsPopover =\n popoverElement === relatedTarget ||\n popoverElement?.contains(relatedTarget);\n const targetIsTrigger =\n triggerElement === relatedTarget ||\n triggerElement?.contains(relatedTarget) ||\n isMouseDown.current;\n\n if (targetIsPopover || targetIsTrigger) {\n return;\n }\n\n onClose?.();\n },\n onKeyDown: (event: React.KeyboardEvent<HTMLDivElement>) => {\n if (_props.onKeyDown) {\n _props.onKeyDown(event);\n }\n\n if (closeOnEsc && event.key === 'Escape') {\n closeAndFocusTrigger();\n }\n },\n }),\n }),\n [\n isOpen,\n renderOnlyWhenOpen,\n popperAttributes,\n popperStyles,\n usePortal,\n popoverId,\n closeOnEsc,\n closeOnBlur,\n popoverElement,\n triggerElement,\n closeAndFocusTrigger,\n onClose,\n ],\n );\n\n return (\n <PopoverContextProvider value={contextValue}>\n {children}\n </PopoverContextProvider>\n );\n}\n\n/**\n * Sets the popover width to the size of the trigger element.\n */\nconst sameWidth: Modifier<'sameWidth', any> = {\n name: 'sameWidth',\n enabled: true,\n phase: 'beforeWrite',\n requires: ['computeStyles'],\n fn: ({ state }) => {\n state.styles.popper.width = `${state.rects.reference.width}px`;\n },\n effect:\n ({ state }) =>\n () => {\n const reference = state.elements.reference as HTMLElement;\n state.elements.popper.style.width = `${reference.offsetWidth}px`;\n },\n};\n","import React, { HTMLProps } from 'react';\n\nexport type PopoverContextType = {\n isOpen: boolean;\n usePortal: boolean;\n renderOnlyWhenOpen: boolean;\n getPopoverProps: (\n _props: HTMLProps<HTMLDivElement>,\n _ref: React.Ref<HTMLDivElement>,\n ) => HTMLProps<HTMLDivElement>;\n getTriggerProps: (\n _props: HTMLProps<HTMLElement>,\n _ref: React.Ref<HTMLElement>,\n ) => HTMLProps<HTMLElement>;\n};\n\nconst PopoverContext = React.createContext<PopoverContextType | undefined>(\n undefined,\n);\n\nexport const usePopoverContext = () => {\n const context = React.useContext(PopoverContext);\n\n if (context === undefined) {\n throw new Error(\n 'usePopoverContext must be used within a PopoverContextProvider',\n );\n }\n\n return context;\n};\n\nexport const PopoverContextProvider = PopoverContext.Provider;\n","import React from 'react';\nimport { cx } from 'emotion';\nimport type {\n CommonProps,\n PropsWithHTMLElement,\n ExpandProps,\n} from '@contentful/f36-core';\nimport { usePopoverContext } from '../PopoverContext';\nimport { Portal } from '@contentful/f36-utils';\nimport { getPopoverContentStyles } from './PopoverContent.styles';\n\ninterface PopoverContentInternalProps extends CommonProps {\n children?: React.ReactNode;\n}\n\nexport type PopoverContentProps = PropsWithHTMLElement<\n PopoverContentInternalProps,\n 'div'\n>;\n\nconst _PopoverContent = (props: ExpandProps<PopoverContentProps>, ref) => {\n const {\n children,\n className,\n testId = 'cf-ui-popover-content',\n role = 'dialog',\n ...otherProps\n } = props;\n const { isOpen, renderOnlyWhenOpen, getPopoverProps, usePortal } =\n usePopoverContext();\n\n const styles = getPopoverContentStyles(isOpen);\n\n const content = (\n <div\n {...otherProps}\n {...getPopoverProps(otherProps, ref)}\n className={cx(styles.container, className)}\n data-test-id={testId}\n tabIndex={-1}\n role={role}\n // specific attribute to mark that this element is absolute positioned\n // for internal contentful apps usage\n data-position-absolute\n >\n {children}\n </div>\n );\n\n if (renderOnlyWhenOpen && !isOpen) {\n return null;\n }\n\n return usePortal ? <Portal>{content}</Portal> : content;\n};\n\nexport const PopoverContent = React.forwardRef(_PopoverContent);\n","import { css } from 'emotion';\nimport tokens from '@contentful/f36-tokens';\n\nexport const getPopoverContentStyles = (isOpen: boolean) => ({\n container: css({\n display: isOpen ? 'initial' : 'none',\n background: tokens.colorWhite,\n border: 0,\n borderRadius: tokens.borderRadiusMedium,\n boxShadow: tokens.boxShadowDefault,\n zIndex: tokens.zIndexDropdown,\n '&:focus': {\n boxShadow: tokens.glowPrimary,\n outline: 'none',\n },\n '&:focus:not(:focus-visible)': {\n boxShadow: tokens.boxShadowDefault,\n },\n }),\n});\n","import React from 'react';\nimport { usePopoverContext } from '../PopoverContext';\n\nexport interface PopoverTriggerProps {\n children: React.ReactNode;\n}\n\n/**\n * PopoverTrigger opens the popover. It must be an interactive element.\n */\nexport const PopoverTrigger = (props: PopoverTriggerProps) => {\n const child = React.Children.only(props.children) as any;\n const { getTriggerProps } = usePopoverContext();\n\n return React.cloneElement(child, {\n ...getTriggerProps(child.props, child.ref),\n 'aria-haspopup': child.props['aria-haspopup'] ?? 'dialog',\n });\n};\n","import { Popover as OriginalPopover } from './Popover';\nimport { PopoverContent } from './PopoverContent/PopoverContent';\nimport { PopoverTrigger } from './PopoverTrigger/PopoverTrigger';\n\ntype CompoundPopover = typeof OriginalPopover & {\n Content: typeof PopoverContent;\n Trigger: typeof PopoverTrigger;\n};\n\nexport const Popover = OriginalPopover as CompoundPopover;\nPopover.Content = PopoverContent;\nPopover.Trigger = PopoverTrigger;\n"]}
|
package/package.json
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contentful/f36-popover",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.1.0",
|
|
4
4
|
"description": "Forma 36: Popover component",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"build": "tsup"
|
|
7
7
|
},
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@contentful/f36-core": "^5.
|
|
10
|
-
"@contentful/f36-tokens": "^
|
|
11
|
-
"@contentful/f36-utils": "^5.
|
|
9
|
+
"@contentful/f36-core": "^5.1.0",
|
|
10
|
+
"@contentful/f36-tokens": "^5.1.0",
|
|
11
|
+
"@contentful/f36-utils": "^5.1.0",
|
|
12
12
|
"@popperjs/core": "^2.11.5",
|
|
13
13
|
"emotion": "^10.0.17",
|
|
14
14
|
"react-popper": "^2.3.0"
|
|
15
15
|
},
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"react": ">=16.8"
|
|
17
|
+
"react": ">=16.8",
|
|
18
|
+
"react-dom": ">=16.8"
|
|
18
19
|
},
|
|
19
20
|
"license": "MIT",
|
|
20
21
|
"files": [
|
|
@@ -28,9 +29,10 @@
|
|
|
28
29
|
"browserslist": "extends @contentful/browserslist-config",
|
|
29
30
|
"repository": {
|
|
30
31
|
"type": "git",
|
|
31
|
-
"url": "https://github.com/contentful/forma-36"
|
|
32
|
+
"url": "git+https://github.com/contentful/forma-36.git"
|
|
32
33
|
},
|
|
33
34
|
"publishConfig": {
|
|
35
|
+
"registry": "https://npm.pkg.github.com/",
|
|
34
36
|
"access": "public"
|
|
35
37
|
}
|
|
36
38
|
}
|