Hoppa till huvudinnehåll

Header

Sidhuvud, Applikationshuvud

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>
Mitt system

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>
Mitt system

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>
Mitt system

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>
)
}
Mitt system

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 tar emot samma props som ett <header>-element, till exempel className.

NameTypeDefaultDescription
primarytrue

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​

NameTypeDefaultDescription
className-
orientationhorizontal

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​

NameTypeDefaultDescription
icon-

The action's icon, 20px, e.g. <Bell size={20} />

children-

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 aria-label

className'react-aria-Button'

The CSS className for the element. A function may be provided to compute the class based on component state.

autoFocus-

Whether the element should receive focus on render.

slot-

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

style-

The inline style for the element. A function may be provided to compute the style based on component state.

form-

The <form> element to associate the button with. The value of this attribute must be the id of a <form> in the same document. See MDN.

isDisabled-

Whether the button is disabled.

type'button'

The behavior of the button when used in an HTML form.

preventFocusOnPress-

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-

The URL that processes the information submitted by the button. Overrides the action attribute of the button's form owner.

formEncType-

Indicates how to encode the form data that is submitted.

formMethod-

Indicates the HTTP method used to submit the form.

formNoValidate-

Indicates that the form is not to be validated when it is submitted.

formTarget-

Overrides the target attribute of the button's form owner.

name-

Submitted as a pair with the button's value as part of the form data.

value-

The value associated with the button's name when it's submitted with the form data.

fullwidthfalse

Adds width: 100%; for the button to span entire width of parent

iconSize20

Adjust icon size

iconPlacement'left'

Display the icon on the left or right side of the button text

isPending-

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.

renderDOMRenderFunction<"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:

  • You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>).
  • Only a single root DOM element can be rendered (no fragments).
  • You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.

MobileMenu​

NameTypeDefaultDescription
title-

A visible title for the menu

className'react-aria-ModalOverlay'

The CSS className for the element. A function may be provided to compute the class based on component state.

slot-

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

style-

The inline style for the element. A function may be provided to compute the style based on component state.

children-

The children of the component. A function may be provided to alter the children based on component state.

renderDOMRenderFunction<"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:

  • You must render the expected element type (e.g. if <button> is expected, you cannot render an <a>).
  • Only a single root DOM element can be rendered (no fragments).
  • You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.
shouldCloseOnInteractOutside-

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-

Whether the modal is currently performing an entry animation.

isExiting-

Whether the modal is currently performing an exit animation.

UNSTABLE_portalContainerElementdocument.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.

isDismissablefalse

Whether to close the modal when the user interacts outside it.

isKeyboardDismissDisabledfalse

Whether pressing the escape key to close the modal should be disabled.

isOpen-

Whether the overlay is open by default (controlled).

defaultOpen-

Whether the overlay is open by default (uncontrolled).