| 1 |
import { useEffect, useRef } from '@wordpress/element'; |
| 2 |
import { __ } from '@wordpress/i18n'; |
| 3 |
import { Icon } from '@wordpress/icons'; |
| 4 |
import classNames from 'classnames'; |
| 5 |
import { |
| 6 |
bannerButtonVariables, |
| 7 |
barVariables, |
| 8 |
buttonHoverClasses, |
| 9 |
colorsOf, |
| 10 |
} from '../colors'; |
| 11 |
import { DismissButton } from '../DismissButton'; |
| 12 |
import { iconFor } from '../icons'; |
| 13 |
import { externalLinkProps } from '../notification-link'; |
| 14 |
|
| 15 |
export const Bar = ({ |
| 16 |
notification, |
| 17 |
href, |
| 18 |
external, |
| 19 |
dismissible, |
| 20 |
onDismiss, |
| 21 |
onClick, |
| 22 |
}) => { |
| 23 |
const { title, content } = notification; |
| 24 |
const ctaLabel = notification['cta-label']; |
| 25 |
const icon = iconFor(notification.icon); |
| 26 |
const colors = colorsOf(notification); |
| 27 |
const ref = useRef(null); |
| 28 |
|
| 29 |
// The agent reads this to keep its panel and the scaled page off the bar. |
| 30 |
useEffect(() => { |
| 31 |
const root = document.documentElement; |
| 32 |
const publish = () => |
| 33 |
root.style.setProperty( |
| 34 |
'--extendify-notification-bar-height', |
| 35 |
// 0 below the breakpoint, where the bar is display:none. |
| 36 |
`${ref.current.offsetHeight}px`, |
| 37 |
); |
| 38 |
|
| 39 |
publish(); |
| 40 |
const observer = new ResizeObserver(publish); |
| 41 |
observer.observe(ref.current); |
| 42 |
return () => { |
| 43 |
observer.disconnect(); |
| 44 |
root.style.removeProperty('--extendify-notification-bar-height'); |
| 45 |
}; |
| 46 |
}, []); |
| 47 |
|
| 48 |
return ( |
| 49 |
<aside |
| 50 |
ref={ref} |
| 51 |
aria-label={ |
| 52 |
// translators: label for the bar reporting whether the site is public. |
| 53 |
__('Site status', 'extendify-local') |
| 54 |
} |
| 55 |
// Below core's admin-bar breakpoint a full-width banner covers the content it reports on. |
| 56 |
className="fixed bottom-0 left-0 right-0 z-higher hidden items-center gap-4 bg-design-main px-5 py-3.5 text-base text-design-text md:flex" |
| 57 |
style={barVariables(colors)} |
| 58 |
data-test="notification-bar" |
| 59 |
> |
| 60 |
{icon && ( |
| 61 |
<span |
| 62 |
className="flex size-10 shrink-0 items-center justify-center rounded-full bg-banner-main text-banner-text" |
| 63 |
data-test="notification-bar-icon" |
| 64 |
> |
| 65 |
<Icon icon={icon} size={24} className="fill-current" /> |
| 66 |
</span> |
| 67 |
)} |
| 68 |
<div className="min-w-0 flex-1"> |
| 69 |
<div className="text-[15px] font-bold">{title}</div> |
| 70 |
<div className="mt-0.5 text-[13px]">{content}</div> |
| 71 |
</div> |
| 72 |
{ctaLabel && href && ( |
| 73 |
<a |
| 74 |
href={href} |
| 75 |
{...externalLinkProps(external)} |
| 76 |
onClick={onClick} |
| 77 |
className={classNames( |
| 78 |
'inline-flex h-10 shrink-0 cursor-pointer items-center rounded-md bg-banner-main px-5 text-sm font-medium text-banner-text no-underline', |
| 79 |
buttonHoverClasses(colors), |
| 80 |
)} |
| 81 |
style={bannerButtonVariables(colors)} |
| 82 |
> |
| 83 |
{ctaLabel} |
| 84 |
</a> |
| 85 |
)} |
| 86 |
{dismissible && ( |
| 87 |
<DismissButton |
| 88 |
onClick={onDismiss} |
| 89 |
size={24} |
| 90 |
className="flex size-8 shrink-0 items-center justify-center rounded-xs text-design-text hover:opacity-80" |
| 91 |
/> |
| 92 |
)} |
| 93 |
</aside> |
| 94 |
); |
| 95 |
}; |
| 96 |
|