{"version":3,"file":"index.mjs","names":[],"sources":["../../src/snackbar/index.tsx"],"sourcesContent":["import type {\n\tFocusEvent,\n\tForwardedRef,\n\tKeyboardEvent,\n\tMouseEvent,\n} from 'react';\nimport clsx from 'clsx';\nimport { useIsPresent } from 'framer-motion';\nimport { speak } from '@wordpress/a11y';\nimport { useMergeRefs } from '@wordpress/compose';\nimport {\n\tuseEffect,\n\tuseLayoutEffect,\n\tuseRef,\n\tforwardRef,\n\trenderToString,\n} from '@wordpress/element';\nimport { __ } from '@wordpress/i18n';\nimport warning from '@wordpress/warning';\nimport Button from '../button';\nimport ExternalLink from '../external-link';\nimport type { SnackbarProps } from './types';\nimport type { NoticeAction } from '../notice/types';\nimport type { WordPressComponentProps } from '../context';\n\nconst NOTICE_TIMEOUT = 6000;\n// Snackbars with an action stay up longer, so there is time to read the\n// message and then use the action.\nconst NOTICE_TIMEOUT_WITH_ACTIONS = 10000;\n\n/**\n * Custom hook which announces the message with the given politeness, if a\n * valid message is provided.\n *\n * @param message    Message to announce.\n * @param politeness Politeness to announce.\n */\nfunction useSpokenMessage(\n\tmessage: SnackbarProps[ 'spokenMessage' ],\n\tpoliteness: NonNullable< SnackbarProps[ 'politeness' ] >\n) {\n\tconst spokenMessage =\n\t\ttypeof message === 'string' ? message : renderToString( message );\n\n\tuseEffect( () => {\n\t\tif ( spokenMessage ) {\n\t\t\tspeak( spokenMessage, politeness );\n\t\t}\n\t}, [ spokenMessage, politeness ] );\n}\n\nfunction UnforwardedSnackbar(\n\t{\n\t\tclassName,\n\t\tchildren,\n\t\tspokenMessage = children,\n\t\tpoliteness = 'polite',\n\t\tactions = [],\n\t\tonRemove,\n\t\ticon = null,\n\t\texplicitDismiss = false,\n\t\t// onDismiss is a callback executed when the snackbar is dismissed.\n\t\t// It is distinct from onRemove, which _looks_ like a callback but is\n\t\t// actually the function to call to remove the snackbar from the UI.\n\t\tonDismiss,\n\t\tlistRef,\n\t}: WordPressComponentProps< SnackbarProps, 'div' >,\n\tref: ForwardedRef< any >\n) {\n\tconst isPresent = useIsPresent();\n\tconst snackbarRef = useRef< HTMLDivElement >( null );\n\tconst mergedRef = useMergeRefs( [ snackbarRef, ref ] );\n\tconst focusReturnRef = useRef< HTMLElement | SVGElement | null >( null );\n\n\tfunction onFocus(\n\t\tevent: FocusEvent< HTMLDivElement, HTMLElement | SVGElement >\n\t) {\n\t\tif ( ! event.currentTarget.contains( event.relatedTarget ) ) {\n\t\t\tfocusReturnRef.current = event.relatedTarget;\n\t\t}\n\t}\n\n\tfunction dismissMe( event: KeyboardEvent | MouseEvent ) {\n\t\tif ( event && event.preventDefault ) {\n\t\t\tevent.preventDefault();\n\t\t}\n\n\t\tif ( listRef?.current ) {\n\t\t\t// Prevent focus loss by moving it to the list element.\n\t\t\tlistRef.current.focus();\n\t\t} else if (\n\t\t\tsnackbarRef.current?.contains(\n\t\t\t\tsnackbarRef.current.ownerDocument.activeElement\n\t\t\t)\n\t\t) {\n\t\t\t// DOM order does not identify a meaningful return target for portals.\n\t\t\tif ( focusReturnRef.current?.isConnected ) {\n\t\t\t\tfocusReturnRef.current.focus( { preventScroll: true } );\n\t\t\t}\n\t\t}\n\n\t\tonDismiss?.();\n\t\tonRemove?.();\n\t}\n\n\tfunction onKeyPress( event: KeyboardEvent< HTMLDivElement > ) {\n\t\t// A key press on the action bubbles up to here. Leave it to the action:\n\t\t// dismissing would cancel the key press, so the action would not run.\n\t\tif ( event.target !== event.currentTarget ) {\n\t\t\treturn;\n\t\t}\n\n\t\tdismissMe( event );\n\t}\n\n\tfunction onActionClick(\n\t\tevent: MouseEvent< HTMLButtonElement | HTMLAnchorElement >,\n\t\tonClick: NoticeAction[ 'onClick' ]\n\t) {\n\t\tevent.stopPropagation();\n\n\t\tonRemove?.();\n\n\t\tif ( onClick ) {\n\t\t\tonClick( event );\n\t\t}\n\t}\n\n\tuseSpokenMessage( spokenMessage, politeness );\n\n\t// The `onDismiss/onRemove` can have unstable references,\n\t// trigger side-effect cleanup, and reset timers.\n\tconst callbacksRef = useRef( { onDismiss, onRemove } );\n\tuseLayoutEffect( () => {\n\t\tcallbacksRef.current = { onDismiss, onRemove };\n\t} );\n\n\tconst timeout =\n\t\tactions.length > 0 ? NOTICE_TIMEOUT_WITH_ACTIONS : NOTICE_TIMEOUT;\n\n\tuseEffect( () => {\n\t\tif ( explicitDismiss || ! isPresent ) {\n\t\t\treturn;\n\t\t}\n\n\t\tconst timeoutHandle = setTimeout( () => {\n\t\t\tcallbacksRef.current.onDismiss?.();\n\t\t\tcallbacksRef.current.onRemove?.();\n\t\t}, timeout );\n\n\t\treturn () => clearTimeout( timeoutHandle );\n\t}, [ explicitDismiss, isPresent, timeout ] );\n\n\tconst classes = clsx( className, 'components-snackbar', {\n\t\t'components-snackbar-explicit-dismiss': !! explicitDismiss,\n\t} );\n\tif ( actions && actions.length > 1 ) {\n\t\t// We need to inform developers that snackbar only accepts 1 action.\n\t\twarning(\n\t\t\t'Snackbar can only have one action. Use Notice if your message requires many actions.'\n\t\t);\n\t\t// return first element only while keeping it inside an array\n\t\tactions = [ actions[ 0 ] ];\n\t}\n\n\tconst snackbarContentClassnames = clsx( 'components-snackbar__content', {\n\t\t'components-snackbar__content-with-icon': !! icon,\n\t} );\n\n\treturn (\n\t\t<div\n\t\t\tref={ mergedRef }\n\t\t\tonFocus={ onFocus }\n\t\t\tclassName={ classes }\n\t\t\tonClick={ ! explicitDismiss ? dismissMe : undefined }\n\t\t\ttabIndex={ 0 }\n\t\t\trole={ ! explicitDismiss ? 'button' : undefined }\n\t\t\tonKeyPress={ ! explicitDismiss ? onKeyPress : undefined }\n\t\t\taria-label={\n\t\t\t\t! explicitDismiss ? __( 'Dismiss this notice' ) : undefined\n\t\t\t}\n\t\t\tdata-testid=\"snackbar\"\n\t\t>\n\t\t\t<div className={ snackbarContentClassnames }>\n\t\t\t\t{ icon && (\n\t\t\t\t\t<div className=\"components-snackbar__icon\">{ icon }</div>\n\t\t\t\t) }\n\t\t\t\t{ children }\n\t\t\t\t{ actions.map(\n\t\t\t\t\t( { label, onClick, url, openInNewTab = false }, index ) =>\n\t\t\t\t\t\turl !== undefined && openInNewTab ? (\n\t\t\t\t\t\t\t<ExternalLink\n\t\t\t\t\t\t\t\tkey={ index }\n\t\t\t\t\t\t\t\thref={ url }\n\t\t\t\t\t\t\t\tonClick={ ( event ) =>\n\t\t\t\t\t\t\t\t\tonActionClick( event, onClick )\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tclassName=\"components-snackbar__action\"\n\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t{ label }\n\t\t\t\t\t\t\t</ExternalLink>\n\t\t\t\t\t\t) : (\n\t\t\t\t\t\t\t<Button\n\t\t\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\t\t\tkey={ index }\n\t\t\t\t\t\t\t\thref={ url }\n\t\t\t\t\t\t\t\tvariant=\"link\"\n\t\t\t\t\t\t\t\tonClick={ (\n\t\t\t\t\t\t\t\t\tevent: MouseEvent< HTMLButtonElement >\n\t\t\t\t\t\t\t\t) => onActionClick( event, onClick ) }\n\t\t\t\t\t\t\t\tclassName=\"components-snackbar__action\"\n\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t{ label }\n\t\t\t\t\t\t\t</Button>\n\t\t\t\t\t\t)\n\t\t\t\t) }\n\t\t\t\t{ explicitDismiss && (\n\t\t\t\t\t<span\n\t\t\t\t\t\trole=\"button\"\n\t\t\t\t\t\taria-label={ __( 'Dismiss this notice' ) }\n\t\t\t\t\t\ttabIndex={ 0 }\n\t\t\t\t\t\tclassName=\"components-snackbar__dismiss-button\"\n\t\t\t\t\t\tonClick={ dismissMe }\n\t\t\t\t\t\tonKeyPress={ dismissMe }\n\t\t\t\t\t>\n\t\t\t\t\t\t&#x2715;\n\t\t\t\t\t</span>\n\t\t\t\t) }\n\t\t\t</div>\n\t\t</div>\n\t);\n}\n\n/**\n * A Snackbar displays a succinct message that is cleared out after a small delay.\n *\n * It can also offer the user options, like viewing a published post.\n * But these options should also be available elsewhere in the UI.\n *\n * ```jsx\n * const MySnackbarNotice = () => (\n *   <Snackbar>Post published successfully.</Snackbar>\n * );\n * ```\n */\nexport const Snackbar = forwardRef( UnforwardedSnackbar );\nSnackbar.displayName = 'Snackbar';\n\nexport default Snackbar;\n"],"mappings":";;;;;;;;;;;AAyBA,MAAM,iBAAiB;AAGvB,MAAM,8BAA8B;;;;;;;;AASpC,SAAS,iBACR,SACA,YACC;CACD,MAAM,gBACL,OAAO,YAAY,WAAW,UAAU,eAAgB,OAAQ;CAEjE,gBAAiB;EAChB,IAAK,eACJ,MAAO,eAAe,UAAW;CAEnC,GAAG,CAAE,eAAe,UAAW,CAAE;AAClC;AAEA,SAAS,oBACR,EACC,WACA,UACA,gBAAgB,UAChB,aAAa,UACb,UAAU,CAAC,GACX,UACA,OAAO,MACP,kBAAkB,OAIlB,WACA,WAED,KACC;CACD,MAAM,YAAY,aAAa;CAC/B,MAAM,cAAc,OAA0B,IAAK;CACnD,MAAM,YAAY,aAAc,CAAE,aAAa,GAAI,CAAE;CACrD,MAAM,iBAAiB,OAA2C,IAAK;CAEvE,SAAS,QACR,OACC;EACD,IAAK,CAAE,MAAM,cAAc,SAAU,MAAM,aAAc,GACxD,eAAe,UAAU,MAAM;CAEjC;CAEA,SAAS,UAAW,OAAoC;EACvD,IAAK,SAAS,MAAM,gBACnB,MAAM,eAAe;EAGtB,IAAK,SAAS,SAEb,QAAQ,QAAQ,MAAM;OAChB,IACN,YAAY,SAAS,SACpB,YAAY,QAAQ,cAAc,aACnC,GAGK;OAAA,eAAe,SAAS,aAC5B,eAAe,QAAQ,MAAO,EAAE,eAAe,KAAK,CAAE;EAAA;EAIxD,YAAY;EACZ,WAAW;CACZ;CAEA,SAAS,WAAY,OAAyC;EAG7D,IAAK,MAAM,WAAW,MAAM,eAC3B;EAGD,UAAW,KAAM;CAClB;CAEA,SAAS,cACR,OACA,SACC;EACD,MAAM,gBAAgB;EAEtB,WAAW;EAEX,IAAK,SAC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