-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace
Toolbar
with UI5 Web Component (#6061)
BREAKING CHANGE: the `Toolbar` component and its related components have been moved to the `@ui5/webcomponents-react-compat` package. BREAKING CHANGE: the `ToolbarV2` component has been renamed to `Toolbar` BREAKING CHANGE: the `ToolbarSpacerV2` component has been renamed to `ToolbarSpacer` BREAKING CHANGE: the `ToolbarSeparatorV2` component has been renamed to `ToolbarSeparator` --------- Co-authored-by: Lukas Harbarth <[email protected]>
- Loading branch information
1 parent
3822bee
commit bf60767
Showing
28 changed files
with
913 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...omponents/OverflowToolbarButton/index.tsx → ...omponents/OverflowToolbarButton/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...nts/OverflowToolbarToggleButton/index.tsx → ...nts/OverflowToolbarToggleButton/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
packages/compat/src/components/Toolbar/OverflowPopover.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,187 @@ | ||
import ButtonDesign from '@ui5/webcomponents/dist/types/ButtonDesign.js'; | ||
import PopoverPlacement from '@ui5/webcomponents/dist/types/PopoverPlacement.js'; | ||
import PopupAccessibleRole from '@ui5/webcomponents/dist/types/PopupAccessibleRole.js'; | ||
import iconOverflow from '@ui5/webcomponents-icons/dist/overflow.js'; | ||
import type { | ||
ButtonPropTypes, | ||
PopoverDomRef, | ||
ToggleButtonDomRef, | ||
ToggleButtonPropTypes | ||
} from '@ui5/webcomponents-react'; | ||
import { Popover, ToggleButton } from '@ui5/webcomponents-react'; | ||
import { useCanRenderPortal } from '@ui5/webcomponents-react/dist/internal/ssr.js'; | ||
import { stopPropagation } from '@ui5/webcomponents-react/dist/internal/stopPropagation.js'; | ||
import { getUi5TagWithSuffix } from '@ui5/webcomponents-react/dist/internal/utils.js'; | ||
import { Device, useSyncRef } from '@ui5/webcomponents-react-base'; | ||
import { clsx } from 'clsx'; | ||
import type { Dispatch, FC, ReactElement, ReactNode, Ref, SetStateAction } from 'react'; | ||
import { cloneElement, useEffect, useRef, useState } from 'react'; | ||
import { createPortal } from 'react-dom'; | ||
import { getOverflowPopoverContext } from '../../internal/OverflowPopoverContext.js'; | ||
import type { ToolbarSeparatorPropTypes } from '../ToolbarSeparator/index.js'; | ||
import type { ToolbarPropTypes } from './index.js'; | ||
|
||
interface OverflowPopoverProps { | ||
lastVisibleIndex: number; | ||
classes: Record<string, string>; | ||
children: ReactNode[]; | ||
portalContainer: Element; | ||
overflowContentRef: Ref<HTMLDivElement>; | ||
numberOfAlwaysVisibleItems?: number; | ||
showMoreText: string; | ||
overflowPopoverRef?: Ref<PopoverDomRef>; | ||
overflowButton?: ReactElement<ToggleButtonPropTypes> | ReactElement<ButtonPropTypes>; | ||
setIsMounted: Dispatch<SetStateAction<boolean>>; | ||
a11yConfig?: ToolbarPropTypes['a11yConfig']; | ||
} | ||
|
||
const isPhone = Device.isPhone(); | ||
|
||
export const OverflowPopover: FC<OverflowPopoverProps> = (props: OverflowPopoverProps) => { | ||
const { | ||
lastVisibleIndex, | ||
classes, | ||
children, | ||
portalContainer, | ||
overflowContentRef, | ||
numberOfAlwaysVisibleItems, | ||
showMoreText, | ||
overflowButton, | ||
overflowPopoverRef, | ||
setIsMounted, | ||
a11yConfig | ||
} = props; | ||
const [pressed, setPressed] = useState(false); | ||
const toggleBtnRef = useRef<ToggleButtonDomRef>(null); | ||
const [componentRef, popoverRef] = useSyncRef(overflowPopoverRef); | ||
|
||
useEffect(() => { | ||
setIsMounted(true); | ||
return () => { | ||
setIsMounted(false); | ||
}; | ||
}, []); | ||
|
||
const handleToggleButtonClick = (e) => { | ||
e.stopPropagation(); | ||
setPressed((prev) => { | ||
if (!prev) { | ||
if (popoverRef.current) { | ||
popoverRef.current.opener = e.target; | ||
} | ||
return true; | ||
} | ||
return false; | ||
}); | ||
}; | ||
|
||
const handleBeforeOpen = () => { | ||
if (toggleBtnRef.current) { | ||
toggleBtnRef.current.accessibilityAttributes = { expanded: true, hasPopup: 'menu' }; | ||
} | ||
}; | ||
const handleAfterOpen = () => { | ||
setPressed(true); | ||
}; | ||
|
||
const handleClose = (e) => { | ||
if (toggleBtnRef.current) { | ||
toggleBtnRef.current.accessibilityAttributes = { expanded: false, hasPopup: 'menu' }; | ||
} | ||
stopPropagation(e); | ||
setPressed(false); | ||
}; | ||
|
||
useEffect(() => { | ||
const tagName = getUi5TagWithSuffix('ui5-toggle-button'); | ||
void customElements.whenDefined(tagName).then(() => { | ||
if (toggleBtnRef.current) { | ||
toggleBtnRef.current.accessibilityAttributes = { expanded: pressed, hasPopup: 'menu' }; | ||
} | ||
}); | ||
}, []); | ||
|
||
const clonedOverflowButtonClick = (e) => { | ||
if (typeof overflowButton?.props?.onClick === 'function') { | ||
overflowButton.props.onClick(e); | ||
} | ||
if (!e.defaultPrevented) { | ||
handleToggleButtonClick(e); | ||
} | ||
}; | ||
|
||
const canRenderPortal = useCanRenderPortal(); | ||
|
||
const accessibleRole = (() => { | ||
if (a11yConfig?.overflowPopover?.contentRole) { | ||
return PopupAccessibleRole.None; | ||
} | ||
return a11yConfig?.overflowPopover?.role; | ||
})(); | ||
|
||
const OverflowPopoverContextProvider = getOverflowPopoverContext().Provider; | ||
|
||
return ( | ||
<OverflowPopoverContextProvider value={{ inPopover: true }}> | ||
{overflowButton ? ( | ||
cloneElement(overflowButton, { onClick: clonedOverflowButtonClick }) | ||
) : ( | ||
<ToggleButton | ||
ref={toggleBtnRef} | ||
design={ButtonDesign.Transparent} | ||
icon={iconOverflow} | ||
onClick={handleToggleButtonClick} | ||
pressed={pressed} | ||
accessibleName={showMoreText} | ||
tooltip={showMoreText} | ||
data-component-name="ToolbarOverflowButton" | ||
/> | ||
)} | ||
{canRenderPortal && | ||
createPortal( | ||
<Popover | ||
data-component-name="ToolbarOverflowPopover" | ||
className={clsx(classes.popover, isPhone && classes.popoverPhone)} | ||
placement={PopoverPlacement.Bottom} | ||
ref={componentRef} | ||
open={pressed} | ||
onClose={handleClose} | ||
onBeforeOpen={handleBeforeOpen} | ||
onOpen={handleAfterOpen} | ||
hideArrow | ||
accessibleRole={accessibleRole} | ||
> | ||
<div | ||
className={classes.popoverContent} | ||
ref={overflowContentRef} | ||
role={a11yConfig?.overflowPopover?.contentRole} | ||
data-component-name="ToolbarOverflowPopoverContent" | ||
> | ||
{children.map((item, index) => { | ||
if (index > lastVisibleIndex && index > numberOfAlwaysVisibleItems - 1) { | ||
// @ts-expect-error: if props is not defined, it doesn't have an id (is not a ReactElement) | ||
if (item?.props?.id) { | ||
// @ts-expect-error: item is ReactElement | ||
return cloneElement(item, { id: `${item.props.id}-overflow` }); | ||
} | ||
// @ts-expect-error: if type is not defined, it's not a spacer | ||
if (item.type?.displayName === 'ToolbarSeparator') { | ||
return cloneElement(item as ReactElement<ToolbarSeparatorPropTypes>, { | ||
style: { | ||
height: '0.0625rem', | ||
margin: '0.375rem 0.1875rem', | ||
width: '100%' | ||
} | ||
}); | ||
} | ||
return item; | ||
} | ||
return null; | ||
})} | ||
</div> | ||
</Popover>, | ||
portalContainer ?? document.body | ||
)} | ||
</OverflowPopoverContextProvider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.