Skip to content

Fix dropdown overlay not staying fixed #3164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/swift-dryers-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gitbook": patch
---

Fix dropdown menu not staying fixed
19 changes: 12 additions & 7 deletions packages/gitbook/src/components/Header/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ export type DropdownButtonProps<E extends HTMLElement = HTMLElement> = Omit<
'ref'
>;

/**
* Button with a dropdown.
*/
export function Dropdown<E extends HTMLElement>(props: {
export type DropdownProps<E extends HTMLElement = HTMLElement> = {
/** Content of the button */
button: (buttonProps: DropdownButtonProps<E>) => React.ReactNode;
/** Content of the dropdown */
children: React.ReactNode;
/** Custom styles */
className?: ClassValue;
}) {
const { button, children, className } = props;
/** Whether to use fixed positioning (for fixed navbars) */
useFixedPosition?: boolean;
Comment on lines +20 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure to understand why we need a special prop for this. The default behaviour of the dropdown is it's fixed and anchored to it's parent no matter what the situation is. Exposing a prop like this leaks implementation details that is not very clear for the caller of this API

};

/**
* Button with a dropdown.
*/
export function Dropdown<E extends HTMLElement = HTMLElement>(props: DropdownProps<E>) {
const { button, children, className, useFixedPosition = false } = props;
const dropdownId = useId();

return (
Expand Down Expand Up @@ -53,7 +57,7 @@ export function Dropdown<E extends HTMLElement>(props: {
className
)}
>
<div className="fixed z-50 w-52">
<div className={tcls(useFixedPosition ? 'fixed' : 'absolute', 'z-50 w-52')}>
<div
className={tcls(
'mt-2',
Expand Down Expand Up @@ -92,6 +96,7 @@ export function DropdownChevron() {
'ms-1',
'transition-all',
'group-hover/dropdown:opacity-11',
'group-hover/dropdown:rotate-180',
'group-focus-within/dropdown:rotate-180'
)}
/>
Expand Down
1 change: 1 addition & 0 deletions packages/gitbook/src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ export function Header(props: { context: GitBookSiteContext; withTopHeader?: boo
siteSpace={siteSpace}
siteSpaces={siteSpaces}
className="w-full grow py-1"
useFixedPosition={true}
/>
</div>
)}
Expand Down
17 changes: 10 additions & 7 deletions packages/gitbook/src/components/Header/SpacesDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ import type { SiteSpace } from '@gitbook/api';
import { tcls } from '@/lib/tailwind';

import type { GitBookSiteContext } from '@v2/lib/context';
import { Dropdown, DropdownChevron, DropdownMenu } from './Dropdown';
import { Dropdown, DropdownChevron, DropdownMenu, type DropdownProps } from './Dropdown';
import { SpacesDropdownMenuItem } from './SpacesDropdownMenuItem';

export function SpacesDropdown(props: {
context: GitBookSiteContext;
siteSpace: SiteSpace;
siteSpaces: SiteSpace[];
className?: string;
}) {
export function SpacesDropdown(
props: {
context: GitBookSiteContext;
siteSpace: SiteSpace;
siteSpaces: SiteSpace[];
className?: string;
} & Partial<DropdownProps>
) {
const { context, siteSpace, siteSpaces, className } = props;
const { linker } = context;

Expand Down Expand Up @@ -67,6 +69,7 @@ export function SpacesDropdown(props: {
<DropdownChevron />
</div>
)}
{...props}
>
<DropdownMenu>
{siteSpaces.map((otherSiteSpace, index) => (
Expand Down