Skip to content

Commit

Permalink
chore(composable menu demos): updated appendTo (#8623)
Browse files Browse the repository at this point in the history
* chore(composable menu demos): updated appendTo

* Updated appendTo based on new Popper logic
  • Loading branch information
thatblindgeye authored Feb 10, 2023
1 parent f6522ca commit b03c06e
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export const ComposableActionsMenu: React.FunctionComponent = () => {
const [selectedItems, setSelectedItems] = React.useState<number[]>([]);
const toggleRef = React.useRef<HTMLButtonElement>(null);
const menuRef = React.useRef<HTMLDivElement>(null);
const containerRef = React.useRef<HTMLDivElement>(null);

const handleMenuKeys = (event: KeyboardEvent) => {
if (isOpen && menuRef.current?.contains(event.target as Node)) {
Expand Down Expand Up @@ -123,15 +122,5 @@ export const ComposableActionsMenu: React.FunctionComponent = () => {
</Menu>
);

return (
<div ref={containerRef}>
<Popper
trigger={toggle}
popper={menu}
isVisible={isOpen}
appendTo={containerRef.current || undefined}
popperMatchesTriggerWidth={false}
/>
</div>
);
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} popperMatchesTriggerWidth={false} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const ComposableApplicationLauncher: React.FunctionComponent = () => {
const [filteredIds, setFilteredIds] = React.useState<string[]>(['*']);
const menuRef = React.useRef<HTMLDivElement>(null);
const toggleRef = React.useRef<HTMLButtonElement>(null);
const containerRef = React.useRef<HTMLDivElement>(null);

const handleMenuKeys = (event: KeyboardEvent) => {
if (!isOpen) {
Expand Down Expand Up @@ -266,15 +265,5 @@ export const ComposableApplicationLauncher: React.FunctionComponent = () => {
</MenuContent>
</Menu>
);
return (
<div ref={containerRef}>
<Popper
trigger={toggle}
popper={menu}
isVisible={isOpen}
popperMatchesTriggerWidth={false}
appendTo={containerRef.current || undefined}
/>
</div>
);
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} popperMatchesTriggerWidth={false} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ export const ComposableContextSelector: React.FunctionComponent = () => {
const menuRef = React.useRef<HTMLDivElement>(null);
const toggleRef = React.useRef<HTMLButtonElement>(null);
const menuFooterBtnRef = React.useRef<HTMLButtonElement>(null);
const containerRef = React.useRef<HTMLDivElement>(null);

const handleMenuKeys = (event: KeyboardEvent) => {
if (!isOpen) {
Expand Down Expand Up @@ -199,15 +198,5 @@ export const ComposableContextSelector: React.FunctionComponent = () => {
</MenuFooter>
</Menu>
);
return (
<div ref={containerRef}>
<Popper
trigger={toggle}
popper={menu}
appendTo={containerRef.current}
isVisible={isOpen}
popperMatchesTriggerWidth={false}
/>
</div>
);
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} popperMatchesTriggerWidth={false} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,21 @@ export const ComposableSimpleDropdown: React.FunctionComponent = () => {
const [selected, setSelected] = React.useState<number>(0);
const toggleRef = React.useRef<HTMLButtonElement>();
const menuRef = React.useRef<HTMLDivElement>();
const containerRef = React.useRef<HTMLDivElement>();

const handleMenuKeys = (event: KeyboardEvent) => {
if (!isOpen) {
return;
}
if (menuRef.current.contains(event.target as Node) || toggleRef.current.contains(event.target as Node)) {
if (menuRef?.current?.contains(event.target as Node) || toggleRef?.current?.contains(event.target as Node)) {
if (event.key === 'Escape' || event.key === 'Tab') {
setIsOpen(!isOpen);
toggleRef.current.focus();
toggleRef?.current?.focus();
}
}
};

const handleClickOutside = (event: MouseEvent) => {
if (isOpen && !menuRef.current.contains(event.target as Node)) {
if (isOpen && !menuRef?.current?.contains(event.target as Node)) {
setIsOpen(false);
}
};
Expand Down Expand Up @@ -110,15 +109,5 @@ export const ComposableSimpleDropdown: React.FunctionComponent = () => {
</MenuContent>
</Menu>
);
return (
<div ref={containerRef}>
<Popper
trigger={toggle}
popper={menu}
appendTo={containerRef.current}
isVisible={isOpen}
popperMatchesTriggerWidth={false}
/>
</div>
);
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} popperMatchesTriggerWidth={false} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ export const ComposableDrilldownMenu: React.FunctionComponent = () => {
const [menuHeights, setMenuHeights] = React.useState<MenuHeightsType>({});
const toggleRef = React.useRef<HTMLButtonElement>(null);
const menuRef = React.useRef<HTMLDivElement>(null);
const containerRef = React.useRef<HTMLDivElement>(null);

const handleMenuKeys = (event: KeyboardEvent) => {
if (isOpen && menuRef.current?.contains(event.target as Node)) {
Expand Down Expand Up @@ -248,15 +247,5 @@ export const ComposableDrilldownMenu: React.FunctionComponent = () => {
</MenuContent>
</Menu>
);
return (
<div ref={containerRef}>
<Popper
trigger={toggle}
popper={menu}
appendTo={containerRef.current || undefined}
isVisible={isOpen}
popperMatchesTriggerWidth={false}
/>
</div>
);
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} popperMatchesTriggerWidth={false} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export const ComposableDropdwnVariants: React.FunctionComponent = () => {
const [toggleSelected, setToggleSelected] = React.useState<string>('basic');
const menuRef = React.useRef<HTMLDivElement>();
const toggleRef = React.useRef<HTMLButtonElement>();
const containerRef = React.useRef<HTMLDivElement>();

const handleToggleSwitch = (selected: boolean, e: React.MouseEvent<any> | React.KeyboardEvent | MouseEvent) => {
setToggleSelected(e.currentTarget.id);
Expand All @@ -33,7 +32,7 @@ export const ComposableDropdwnVariants: React.FunctionComponent = () => {
if (isOpen && menuRef && menuRef.current && menuRef.current.contains(event.target as Node)) {
if (event.key === 'Escape' || event.key === 'Tab') {
setIsOpen(!isOpen);
toggleRef.current.focus();
toggleRef?.current?.focus();
}
}
};
Expand Down Expand Up @@ -227,15 +226,12 @@ export const ComposableDropdwnVariants: React.FunctionComponent = () => {
/>
</ToggleGroup>
<br />
<div ref={containerRef}>
<Popper
trigger={buildToggle()}
popper={menu}
appendTo={containerRef.current}
isVisible={isOpen}
popperMatchesTriggerWidth={['image', 'checkbox'].includes(toggleSelected)}
/>
</div>
<Popper
trigger={buildToggle()}
popper={menu}
isVisible={isOpen}
popperMatchesTriggerWidth={['image', 'checkbox'].includes(toggleSelected)}
/>
</React.Fragment>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const ComposableFlyout: React.FunctionComponent = () => {
const [isOpen, setIsOpen] = React.useState<boolean>(false);
const menuRef = React.useRef<HTMLDivElement>(null);
const toggleRef = React.useRef<HTMLButtonElement>(null);
const containerRef = React.useRef<HTMLDivElement>(null);

const handleMenuKeys = (event: KeyboardEvent) => {
if (!isOpen) {
Expand Down Expand Up @@ -101,15 +100,5 @@ export const ComposableFlyout: React.FunctionComponent = () => {
</Menu>
);

return (
<div ref={containerRef}>
<Popper
trigger={toggle}
popper={menu}
appendTo={containerRef.current || undefined}
isVisible={isOpen}
popperMatchesTriggerWidth={false}
/>
</div>
);
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} popperMatchesTriggerWidth={false} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const ComposableMultipleTypeaheadSelect: React.FunctionComponent = () =>
const [focusedItemIndex, setFocusedItemIndex] = React.useState<number | null>(null);
const [activeItem, setActiveItem] = React.useState<string | null>(null);
const [selected, setSelected] = React.useState<string[]>([]);
const containerRef = React.useRef<HTMLDivElement>();
const menuToggleRef = React.useRef<MenuToggleElement>({} as MenuToggleElement);
const textInputRef = React.useRef<HTMLInputElement>();
const menuRef = React.useRef<HTMLDivElement>();
Expand Down Expand Up @@ -241,15 +240,12 @@ export const ComposableMultipleTypeaheadSelect: React.FunctionComponent = () =>
</Menu>
);
return (
<div ref={containerRef as React.Ref<HTMLDivElement>}>
<Popper
trigger={toggle}
popper={menu}
appendTo={containerRef.current}
isVisible={isMenuOpen}
onDocumentClick={onDocumentClick}
onDocumentKeyDown={onDocumentKeydown}
/>
</div>
<Popper
trigger={toggle}
popper={menu}
isVisible={isMenuOpen}
onDocumentClick={onDocumentClick}
onDocumentKeyDown={onDocumentKeydown}
/>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ export const ComposableOptionsMenuVariants: React.FunctionComponent = () => {
const [selected, setSelected] = React.useState<string>('');
const menuRef = React.useRef<HTMLDivElement>();
const toggleRef = React.useRef<HTMLButtonElement>();
const containerRef = React.useRef<HTMLDivElement>();

const handleMenuKeys = (event: KeyboardEvent) => {
if (isOpen && menuRef.current.contains(event.target as Node)) {
if (isOpen && menuRef?.current?.contains(event.target as Node)) {
if (event.key === 'Escape' || event.key === 'Tab') {
setIsOpen(!isOpen);
toggleRef.current.focus();
toggleRef?.current?.focus();
}
}
};

const handleClickOutside = (event: MouseEvent) => {
if (isOpen && !menuRef.current.contains(event.target as Node)) {
if (isOpen && !menuRef?.current?.contains(event.target as Node)) {
setIsOpen(false);
}
};
Expand Down Expand Up @@ -93,9 +92,5 @@ export const ComposableOptionsMenuVariants: React.FunctionComponent = () => {
</MenuContent>
</Menu>
);
return (
<div ref={containerRef}>
<Popper trigger={toggle} popper={menu} appendTo={containerRef.current} isVisible={isOpen} />
</div>
);
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -97,5 +97,5 @@ export const ComposableSimpleCheckboxSelect: React.FunctionComponent = () => {
</MenuContent>
</Menu>
);
return <Popper appendTo={toggleRef.current || undefined} trigger={toggle} popper={menu} isVisible={isOpen} />;
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export const ComposableSimpleDropdown: React.FunctionComponent = () => {
const [isOpen, setIsOpen] = React.useState(false);
const toggleRef = React.useRef<HTMLButtonElement>(null);
const menuRef = React.useRef<HTMLDivElement>(null);
const containerRef = React.useRef<HTMLDivElement>(null);

const handleMenuKeys = (event: KeyboardEvent) => {
if (!isOpen) {
Expand Down Expand Up @@ -67,9 +66,5 @@ export const ComposableSimpleDropdown: React.FunctionComponent = () => {
</MenuContent>
</Menu>
);
return (
<div ref={containerRef}>
<Popper trigger={toggle} popper={menu} appendTo={containerRef.current || undefined} isVisible={isOpen} />
</div>
);
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export const ComposableSimpleSelect: React.FunctionComponent = () => {
const [selected, setSelected] = React.useState<string>('Select a value');
const toggleRef = React.useRef<HTMLButtonElement>(null);
const menuRef = React.useRef<HTMLDivElement>(null);
const containerRef = React.useRef<HTMLDivElement>(null);

const handleMenuKeys = (event: KeyboardEvent) => {
if (isOpen && menuRef.current?.contains(event.target as Node)) {
Expand Down Expand Up @@ -80,9 +79,5 @@ export const ComposableSimpleSelect: React.FunctionComponent = () => {
</MenuContent>
</Menu>
);
return (
<div ref={containerRef}>
<Popper trigger={toggle} popper={menu} appendTo={containerRef.current || undefined} isVisible={isOpen} />
</div>
);
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const ComposableTreeViewMenu: React.FunctionComponent = () => {
const [isOpen, setIsOpen] = React.useState<boolean>(false);
const [checkedItems, setCheckedItems] = React.useState<TreeViewDataItem[]>([]);
const toggleRef = React.useRef<HTMLButtonElement>(null);
const containerRef = React.useRef<HTMLDivElement>(null);
const menuRef = React.useRef<HTMLDivElement>();

const statusOptions: TreeViewDataItem[] = [
Expand Down Expand Up @@ -266,15 +265,5 @@ export const ComposableTreeViewMenu: React.FunctionComponent = () => {
</PanelMain>
</Panel>
);
return (
<div ref={containerRef}>
<Popper
trigger={toggle}
popper={menu}
isVisible={isOpen}
appendTo={containerRef.current || undefined}
popperMatchesTriggerWidth={false}
/>
</div>
);
return <Popper trigger={toggle} popper={menu} isVisible={isOpen} popperMatchesTriggerWidth={false} />;
};
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,6 @@ export const ComposableTypeaheadSelect: React.FunctionComponent = () => {
<Popper
trigger={
<MenuToggle
// Needed to append the menu closer to the toggle in DOM
id="temp-toggle-id"
variant="typeahead"
onClick={toggleMenuOpen}
innerRef={menuToggleRef}
Expand Down Expand Up @@ -229,7 +227,6 @@ export const ComposableTypeaheadSelect: React.FunctionComponent = () => {
isVisible={isMenuOpen}
onDocumentClick={onDocumentClick}
onDocumentKeyDown={onDocumentKeydown}
appendTo={() => document.getElementById('temp-toggle-id')}
/>
);
};

0 comments on commit b03c06e

Please sign in to comment.