import React from 'react' import styled from 'styled-components' import { RovingTabIndexProvider } from '../react-roving-tabindex' import { spacing, align, size, theme, shadow, useResizeObserver, } from '@planview/pv-utilities' import { getBreakPoint, shouldDisplayAtBreakpoint } from '../utils' import type { ResizeContextType } from '../utils/context' import ResizeContext from '../utils/context' import type { ToolbarSectionRightProps, ToolbarSectionLeft } from '../section' export const ToolbarContainerElement = styled.div` position: relative; background-color: ${theme.backgroundNeutral50}; height: ${size.medium}px; min-height: ${size.medium}px; width: 100%; z-index: 10; ${align.centerV}; flex-direction: row; justify-content: space-between; margin: 0; ${shadow.small}; box-sizing: border-box; padding: 0 ${spacing.small}px; ` export type BaseToolbarContainerProps = { /** Content of toolbar */ children: React.ReactNode /** a11y: describe what the toolbar is controlling (e.g. "Boards tool") */ 'aria-label'?: string } & Omit, 'children' | 'aria-label'> const ToolbarContainerImpl: React.ForwardRefRenderFunction< HTMLDivElement, BaseToolbarContainerProps > = ({ children, 'aria-label': ariaLabel, ...otherProps }, ref) => ( {children} ) export const BaseToolbarContainer = React.forwardRef(ToolbarContainerImpl) export type ToolbarProps = { /** Toolbar-compatible children (the ones exported from this library). If mixing with custom elements, make sure they use display: flex */ children: | React.ReactElement> | [ React.ReactElement< React.ComponentProps >, React.ReactElement, ] /** class name to support styling */ className?: string /** a11y: aria-label on wrapping element using role="toolbar" */ label: string } const ToolbarImpl = ({ children, className, label }: ToolbarProps) => { const ref = React.useRef(null) const { width } = useResizeObserver({ ref }) const { breakpoint, showLabels, showSeparators } = getBreakPoint(width) const initialized = !!width const resizeContext = React.useMemo( () => ({ initialized, breakpoint, showLabels, showSeparators, shouldDisplay: (displayOn) => shouldDisplayAtBreakpoint(breakpoint, displayOn), }), [breakpoint, showLabels, showSeparators, initialized] ) return ( {children} ) } /** * * `import { Toolbar } from '@planview/pv-toolbar'` * * This is a strict implementation of the [Toolbar from the Planview Design system](https://design.planview.com/components/toolbar/toolbar). The component adopts the [WAI-ARIA toolbar pattern](https://www.w3.org/WAI/ARIA/apg/patterns/toolbar/) implementing roving tabindex and enforces certain design-system specific patterns. */ export const Toolbar = React.memo(ToolbarImpl)