Skip to content

Commit

Permalink
UI: Support custom <Banner /> height
Browse files Browse the repository at this point in the history
  • Loading branch information
fuma-nama committed Jan 11, 2025
1 parent 056ab2c commit ca1cf19
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/thirty-buckets-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'fumadocs-ui': patch
---

Support custom `<Banner />` height
24 changes: 17 additions & 7 deletions packages/ui/src/components/banner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@ export function Banner({
id,
variant = 'normal',
changeLayout = true,
height = '3rem',
...props
}: HTMLAttributes<HTMLDivElement> & {
/**
* @defaultValue 3rem
*/
height?: string;

/**
* @defaultValue 'normal'
*/
Expand All @@ -22,9 +28,9 @@ export function Banner({
* @defaultValue true
*/
changeLayout?: boolean;
}): React.ReactElement {
}) {
const [open, setOpen] = useState(true);
const globalKey = id ? `nd-banner-${id}` : undefined;
const globalKey = id ? `nd-banner-${id}` : null;

useEffect(() => {
if (globalKey) setOpen(localStorage.getItem(globalKey) !== 'true');
Expand All @@ -35,26 +41,30 @@ export function Banner({
if (globalKey) localStorage.setItem(globalKey, 'true');
}, [globalKey]);

if (!open) return null;

return (
<div
id={id}
{...props}
className={cn(
'sticky top-0 z-40 flex h-12 flex-row items-center justify-center bg-fd-secondary px-4 text-center text-sm font-medium',
'sticky top-0 z-40 flex h-[var(--fd-banner-height)] flex-row items-center justify-center bg-fd-secondary px-4 text-center text-sm font-medium',
variant === 'rainbow' && 'bg-fd-background',
!open && 'hidden',
props.className,
)}
>
{changeLayout && open ? (
<style>{`
:root:not(.${globalKey ?? 'nd-banner-never'}) { --fd-banner-height: 3rem; }
`}</style>
<style>
{globalKey
? `:root:not(.${globalKey}) { --fd-banner-height: ${height}; }`
: `:root { --fd-banner-height: ${height}; }`}
</style>
) : null}
{globalKey ? (
<style>{`.${globalKey} #${id} { display: none; }`}</style>
) : null}
{id ? (
{globalKey ? (
<script
dangerouslySetInnerHTML={{
__html: `if (localStorage.getItem('${globalKey}') === 'true') document.documentElement.classList.add('${globalKey}');`,
Expand Down

0 comments on commit ca1cf19

Please sign in to comment.