Header
Sidhuvud med logotyp, navigation på små skärmar samt plats för globala handlingar. Renderar ett semantiskt <header>-element och tillhandahåller kontext för logotypstorlek (responsiv).
import {
Header,
HeaderActions,
HeaderAction,
HeaderLogo,
HeaderTitle,
MobileMenu,
Navigation,
NavigationItem,
NavigationLink,
} from '@midas-ds/layout'
import { Bell, House } from 'lucide-react'
<Header>
<MobileMenu title='Mitt system'>
<Navigation>
<NavigationItem>
<NavigationLink
href='/'
icon={<House />}
isActive
>
Hem
</NavigationLink>
</NavigationItem>
</Navigation>
</MobileMenu>
<HeaderLogo />
<HeaderTitle>Mitt system</HeaderTitle>
<HeaderActions>
<HeaderAction icon={<Bell size={20} />}>Notiser</HeaderAction>
</HeaderActions>
</Header>
Logotyp
Logotypen läggs till i Header via <HeaderLogo>. Header hanterar responsiv storlek automatiskt och visar en kompakt variant på små skärmar (< 768px). Logotypen kan länka till startsidan genom att wrappas i den routing-komponent som används i applikationen.
// No router
<a href='/'>
<HeaderLogo />
</a>
// Next.js
import Link from 'next/link'
<Link href='/'>
<HeaderLogo />
</Link>
// React Router
import { Link } from 'react-router'
<Link to='/'>
<HeaderLogo />
</Link>
Globala handlingar
I Header läggs globala handlingar, alltså handlingar som användaren ska kunna komma åt från alla sidor i applikationen. Typiska globala handlingar är Logga ut och Inställningar. Dessa läggs till i Header via <HeaderActions>. Varje global handling ska ha en ikon och en beskrivande etikett.
<Header>
<HeaderLogo />
<HeaderTitle>Mitt system</HeaderTitle>
<HeaderActions>
<HeaderAction icon={<Bell size={20} />}>Notiser</HeaderAction>
<HeaderAction icon={<User size={20} />}>Min profil</HeaderAction>
<HeaderAction icon={<Settings size={20} />}>Inställningar</HeaderAction>
</HeaderActions>
</Header>
Varianter
HeaderActions finns i två varianter: horisontell med ikonen till vänster om etiketten, och vertikal med etiketten under ikonen.
Horisontell
Standardvarianten för globala handlingar. På små skärmar (< 768px) döljs etiketterna så att bara ikonerna visas.
Vertikal
Variant för tjänster som ingår i den externa kundresan och ska likna migrationsverket.se. Med orientation='vertical' hamnar etiketten under ikonen. Den vertikala varianten visar alltid etiketterna, även på små skärmar. Det är upp till applikationen att begränsa antalet handlingar eller vid behov utelämna HeaderTitle så att alla globala handlingar får plats på skärmen.
<HeaderActions orientation='vertical'>
<HeaderAction icon={<Bell size={20} />}>Notiser</HeaderAction>
<HeaderAction icon={<User size={20} />}>Min profil</HeaderAction>
<HeaderAction icon={<Settings size={20} />}>Inställningar</HeaderAction>
</HeaderActions>
Overflow
HeaderActions hanterar inte overflow automatiskt. Om applikationen har många globala handlingar behöver dessa läggas i en overflow-meny på små skärmar. Det görs via en mediaquery. Prova att ändra bredden på fönstret nedan för att se beteendet:
import { useMediaQuery } from '@react-spectrum/utils'
import { Menu, MenuItem, MenuPopover, MenuTrigger } from '@midas-ds/components'
import { Header, HeaderActions, HeaderAction, HeaderLogo, HeaderTitle } from '@midas-ds/layout'
import { Bell, Ellipsis, Settings, User } from 'lucide-react'
const collapsibleActions = [
{ id: 'profile', label: 'Min profil', icon: <User size={20} /> },
{ id: 'settings', label: 'Inställningar', icon: <Settings size={20} /> },
]
function AppHeader() {
// Välj den breakpoint som passar din app
const isNarrow = useMediaQuery('(max-width: 700px)')
return (
<Header>
<HeaderLogo />
<HeaderTitle>Mitt system</HeaderTitle>
<HeaderActions>
<HeaderAction icon={<Bell size={20} />}>Notiser</HeaderAction>
{isNarrow ? (
<MenuTrigger>
<HeaderAction
aria-label='Fler alternativ'
icon={<Ellipsis size={20} />}
/>
<MenuPopover>
<Menu>
{collapsibleActions.map(action => (
<MenuItem
key={action.id}
id={action.id}
>
{action.label}
</MenuItem>
))}
</Menu>
</MenuPopover>
</MenuTrigger>
) : (
collapsibleActions.map(action => (
<HeaderAction
key={action.id}
icon={action.icon}
>
{action.label}
</HeaderAction>
))
)}
</HeaderActions>
</Header>
)
}
Navigering på små skärmar/fönster
Navigering på små skärmar/webbläsarfönster (< 768px) görs via Header och komponenten MobileMenu. MobileMenu renderar ingenting på desktop eftersom den är villkorsstyrd via useIsMobileDevice(). Det är därmed säkert att alltid inkludera den i trädet.
import {
Header,
HeaderLogo,
HeaderTitle,
HeaderActions,
HeaderAction,
MobileMenu,
Navigation,
NavigationItem,
NavigationLink,
} from '@midas-ds/layout'
import { Bell, User, House } from 'lucide-react'
export const MobileHeaderExample = () => {
return (
<Header>
<MobileMenu title='Mitt system'>
<Navigation>
<NavigationItem>
<NavigationLink
href='/'
icon={<House />}
isActive
>
Hem
</NavigationLink>
</NavigationItem>
</Navigation>
</MobileMenu>
<HeaderLogo />
<HeaderTitle>Mitt system</HeaderTitle>
<HeaderActions>
<HeaderAction icon={<Bell size={20} />}>Notiser</HeaderAction>
<HeaderAction icon={<User size={20} />}>Min profil</HeaderAction>
</HeaderActions>
</Header>
)
}
API
Header
Header tar emot samma props som ett <header>-element, till exempel className.
HeaderLogo
| Name | Type | Default | Description |
|---|---|---|---|
primary | boolean | true | Shows the logo in its primary colour, set to false for the monochrome version |
HeaderTitle
HeaderTitle tar emot samma props som ett <span>-element, till exempel className.
HeaderActions
| Name | Type | Default | Description |
|---|---|---|---|
className | string | - | |
orientation | "horizontal" | "vertical" | horizontal | Places each action's label next to its icon, or below it. On small screens horizontal actions show only their icons, while vertical actions keep their labels |
HeaderAction
| Name | Type | Default | Description |
|---|---|---|---|
icon | ReactNode | - | The action's icon, 20px, e.g. |
children | ChildrenOrFunction<ButtonRenderProps> | - | The label. In horizontal HeaderActions it's hidden on small screens,
where only the icon shows, but screen readers still read it. Without a
label, give the action an |
className | ClassNameOrFunction<ButtonRenderProps> | 'react-aria-Button' | The CSS className for the element. A function may be provided to compute the class based on component state. |
autoFocus | boolean | - | Whether the element should receive focus on render. |
slot | string | null | - | A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit |
style | StyleOrFunction<ButtonRenderProps> | - | The inline style for the element. A function may be provided to compute the style based on component state. |
form | string | - | The |
isDisabled | boolean | - | Whether the button is disabled. |
type | "button" | "reset" | "submit" | 'button' | The behavior of the button when used in an HTML form. |
preventFocusOnPress | boolean | - | Whether to prevent focus from moving to the button when pressing it. Caution, this can make the button inaccessible and should only be used when alternative keyboard interaction is provided, such as ComboBox's MenuTrigger or a NumberField's increment/decrement control. |
formAction | ((formData: FormData) => void | Promise<void>) | string | - | The URL that processes the information submitted by the button. Overrides the action attribute of the button's form owner. |
formEncType | string | - | Indicates how to encode the form data that is submitted. |
formMethod | string | - | Indicates the HTTP method used to submit the form. |
formNoValidate | boolean | - | Indicates that the form is not to be validated when it is submitted. |
formTarget | string | - | Overrides the target attribute of the button's form owner. |
name | string | - | Submitted as a pair with the button's value as part of the form data. |
value | string | - | The value associated with the button's name when it's submitted with the form data. |
fullwidth | boolean | false | Adds width: 100%; for the button to span entire width of parent |
iconSize | number | 20 | Adjust icon size |
iconPlacement | "left" | "right" | 'left' | Display the icon on the left or right side of the button text |
isPending | boolean | - | Whether the button is in a pending state. This disables press and hover events while retaining focusability, and announces the pending state to screen readers. |
render | DOMRenderFunction<"button", ButtonRenderProps> | - | Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements:
|
MobileMenu
| Name | Type | Default | Description |
|---|---|---|---|
title | string | - | A visible title for the menu |
className | ClassNameOrFunction<ModalRenderProps> | 'react-aria-ModalOverlay' | The CSS className for the element. A function may be provided to compute the class based on component state. |
slot | string | null | - | A slot name for the component. Slots allow the component to receive props from a parent
component. An explicit |
style | StyleOrFunction<ModalRenderProps> | - | The inline style for the element. A function may be provided to compute the style based on component state. |
children | ChildrenOrFunction<ModalRenderProps> | - | The children of the component. A function may be provided to alter the children based on component state. |
render | DOMRenderFunction<"div", ModalRenderProps> | - | Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components. Requirements:
|
shouldCloseOnInteractOutside | ((element: Element) => boolean) | - | When user interacts with the argument element outside of the overlay ref, return true if onClose should be called. This gives you a chance to filter out interaction with elements that should not dismiss the overlay. By default, onClose will always be called on interaction outside the overlay ref. |
isEntering | boolean | - | Whether the modal is currently performing an entry animation. |
isExiting | boolean | - | Whether the modal is currently performing an exit animation. |
UNSTABLE_portalContainer | Element | document.body | The container element in which the overlay portal will be placed. This may have unknown behavior depending on where it is portalled to. @deprecated - Use a parent UNSAFE_PortalProvider to set your portal container instead. |
isDismissable | boolean | false | Whether to close the modal when the user interacts outside it. |
isKeyboardDismissDisabled | boolean | false | Whether pressing the escape key to close the modal should be disabled. |
isOpen | boolean | - | Whether the overlay is open by default (controlled). |
defaultOpen | boolean | - | Whether the overlay is open by default (uncontrolled). |