Skip to content
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
6 changes: 4 additions & 2 deletions src/components/common/Layout/Header/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import type { ReactNode } from 'react'
import type { ReactNode, ForwardedRef } from 'react'
import * as S from './styled'
import { Typography as T } from '../../../../core/typography'

type Props = {
title: string
subtitle?: string
actions?: ReactNode
forwardedRef?: ForwardedRef<HTMLDivElement>
}

export default function BaseLayout({
title = 'Title',
subtitle = '',
actions,
forwardedRef,
}: Props) {
return (
<S.Header role="banner">
<S.Header role="banner" ref={forwardedRef}>
<div>
<T.Heading3 bold>{title}</T.Heading3>
<T.BodySmall>{subtitle}</T.BodySmall>
Expand Down
15 changes: 12 additions & 3 deletions src/components/common/organisms/Tabs/Tabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ export type TabItem = {
content: React.ReactNode
}

type Props = { items: TabItem[]; defaultKey?: string }
type Props = {
items: TabItem[]
defaultKey?: string
offset: number
}

export default function Tabs({ items, defaultKey }: Props) {
export default function Tabs({ items, defaultKey, offset }: Props) {
const keys = items.map((i) => i.key)
const [active, setActive] = useState(defaultKey ?? keys[0])

Expand All @@ -28,7 +32,12 @@ export default function Tabs({ items, defaultKey }: Props) {

return (
<>
<S.Bar role="tablist" aria-label="Sections" onKeyDown={onKeyDown}>
<S.Bar
role="tablist"
aria-label="Sections"
onKeyDown={onKeyDown}
offset={offset} // <-- now forwarded
>
{items.map((t) => {
const isActive = t.key === active
return (
Expand Down
7 changes: 6 additions & 1 deletion src/components/common/organisms/Tabs/styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ import tokens from '../../../../core/tokens'
const DIVIDER_W = `${tokens.size.BASELINE / 8}px`
const INDICATOR_H = '2px'

export const Bar = styled.nav`
export const Bar = styled.nav<{ offset: number }>`
display: flex;
gap: ${tokens.gap.LARGE}px;
margin: ${tokens.spacing.BASELINE * 2}px 0 ${tokens.spacing.BASELINE}px;
border-bottom: ${DIVIDER_W} solid ${({ theme }) => theme.colors.scrollBar};
background: ${({ theme }) => theme.colors.primaryBackground};

position: sticky;
top: ${({ offset }) => offset}px;
z-index: 10;
`

export const TabButton = styled.button<{ active: boolean }>`
Expand Down
19 changes: 19 additions & 0 deletions src/hooks/useElementHeight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useLayoutEffect, useRef, useState } from 'react'

export function useElementHeight<T extends HTMLElement>() {
const ref = useRef<T>(null)
const [height, setHeight] = useState(0)

useLayoutEffect(() => {
if (!ref.current) return

const updateHeight = () => setHeight(ref.current?.offsetHeight || 0)

updateHeight()

window.addEventListener('resize', updateHeight)
return () => window.removeEventListener('resize', updateHeight)
}, [])

return { ref, height }
}
6 changes: 5 additions & 1 deletion src/pages/LiberyPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ import Tabs from '../../components/common/organisms/Tabs'
import { PaddedContainer } from '../../components/common/styled'
import { LIBRARY_TABS } from './tabs.config'
import Header from '../../components/common/Layout/Header'
import { useElementHeight } from '../../hooks/useElementHeight'

export default function LibraryPage() {
const { ref, height } = useElementHeight<HTMLDivElement>()

return (
<>
<Header
title="React-Vite Template"
subtitle="Explore reusable components, hooks, and patterns"
forwardedRef={ref}
/>

<PaddedContainer>
<Tabs items={LIBRARY_TABS} defaultKey="overview" />
<Tabs offset={height} items={LIBRARY_TABS} defaultKey="overview" />
</PaddedContainer>
</>
)
Expand Down