Skip to content

Commit

Permalink
- Add zIndex and appendTo props to ControlledPopover.
Browse files Browse the repository at this point in the history
- NavBarPopoverButton now toggles the popover, so if the user clicks it when it is open, it closes.
- NavBarButton now forwards ref, so that it works with Popover anchoring.
  • Loading branch information
mattias800 committed Sep 18, 2024
1 parent c04ae01 commit 5d10640
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
32 changes: 16 additions & 16 deletions packages/panels/src/components/nav-bar/NavBarButton.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { FlatButton, FlatButtonProps } from "@stenajs-webui/elements";
import * as React from "react";
import { forwardRef } from "react";
import cx from "classnames";
import styles from "./NavBarButton.module.css";

export interface NavBarButtonProps extends FlatButtonProps {
selected?: boolean;
}

export const NavBarButton: React.FC<NavBarButtonProps> = ({
selected,
className,
...buttonProps
}) => {
return (
<FlatButton
{...buttonProps}
className={cx(
styles.navBarButton,
selected && styles.selected,
className
)}
/>
);
};
export const NavBarButton = forwardRef<HTMLButtonElement, NavBarButtonProps>(
function ({ selected, className, ...buttonProps }, ref) {
return (
<FlatButton
{...buttonProps}
ref={ref}
className={cx(
styles.navBarButton,
selected && styles.selected,
className
)}
/>
);
}
);
27 changes: 13 additions & 14 deletions packages/panels/src/components/nav-bar/NavBarPopoverButton.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from "react";
import { ReactNode } from "react";
import { NavBarButton, NavBarButtonProps } from "./NavBarButton";
import { Popover } from "@stenajs-webui/tooltip";
import { Box, Row } from "@stenajs-webui/core";
import { ControlledPopover } from "@stenajs-webui/tooltip";
import { Box, useBoolean } from "@stenajs-webui/core";

type RenderProp = (args: RenderPropArgs) => ReactNode;

Expand All @@ -20,21 +20,20 @@ export const NavBarPopoverButton: React.FC<NavBarPopoverButtonProps> = ({
children,
...navBarButtonProps
}) => {
const [isOpen, , close, toggle] = useBoolean(false);

return (
<Popover
<ControlledPopover
renderTrigger={(props) => (
<Row {...props}>
<NavBarButton {...navBarButtonProps} />
</Row>
<NavBarButton {...navBarButtonProps} {...props} onClick={toggle} />
)}
trigger={"click"}
open={isOpen}
onRequestClose={close}
>
{({ onRequestClose }) => (
<Box>
{content && content({ close: onRequestClose })}
{children}
</Box>
)}
</Popover>
<Box>
{content && content({ close })}
{children}
</Box>
</ControlledPopover>
);
};
8 changes: 6 additions & 2 deletions packages/tooltip/src/components/popover/ControlledPopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export interface ControlledPopoverProps extends PropsWithChildren {
disablePadding?: boolean;
restoreFocus?: boolean;
returnFocus?: boolean;
appendTo?: HTMLElement | null | React.MutableRefObject<HTMLElement | null>;
zIndex?: number;
}

const ARROW_WIDTH = 12;
Expand All @@ -44,6 +46,8 @@ export const ControlledPopover: React.FC<ControlledPopoverProps> = ({
onRequestClose,
restoreFocus,
returnFocus,
appendTo,
zIndex,
}) => {
const arrowRef = useRef(null);

Expand Down Expand Up @@ -88,7 +92,7 @@ export const ControlledPopover: React.FC<ControlledPopoverProps> = ({
{renderTrigger({ ref: refs.setReference, ...getReferenceProps() })}

{isMounted && (
<FloatingPortal>
<FloatingPortal root={appendTo}>
<FloatingFocusManager
context={context}
modal={false}
Expand All @@ -97,7 +101,7 @@ export const ControlledPopover: React.FC<ControlledPopoverProps> = ({
>
<div
ref={refs.setFloating}
style={floatingStyles}
style={{ zIndex, ...floatingStyles }}
{...getFloatingProps}
>
<div
Expand Down

0 comments on commit 5d10640

Please sign in to comment.