✨ Add TextField and Alert components
TextField supports label, description, error state and hidden labels with proper aria wiring. Alert ships the four Aksel variants with matching icons from @navikt/aksel-icons. Adds stylesheets for both.
This commit is contained in:
1 parent
1bcf7f5d53
commit
7ff29efb63
9 files changed
+286
No files matched your search
@@ -0,0 +1,73 @@
|
|||||||
|
/**
|
||||||
|
* Alert
|
||||||
|
*/
|
||||||
|
|
||||||
|
.navds-alert {
|
||||||
|
display: flex;
|
||||||
|
align-items: flex-start;
|
||||||
|
gap: var(--a-spacing-3);
|
||||||
|
padding: var(--a-spacing-4);
|
||||||
|
border-radius: var(--a-border-radius-medium);
|
||||||
|
border: 1px solid transparent;
|
||||||
|
font-family: var(--a-font-family);
|
||||||
|
color: var(--a-text-default);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-alert__icon {
|
||||||
|
flex-shrink: 0;
|
||||||
|
font-size: 1.5rem;
|
||||||
|
margin-top: 0.125rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-alert__content {
|
||||||
|
font-size: var(--a-font-size-md);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-alert--small {
|
||||||
|
padding: var(--a-spacing-2) var(--a-spacing-3);
|
||||||
|
gap: var(--a-spacing-2);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-alert--small .navds-alert__icon {
|
||||||
|
font-size: 1.25rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-alert--full-width {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-alert--info {
|
||||||
|
background-color: var(--a-surface-info-moderate);
|
||||||
|
border-color: var(--a-icon-action);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-alert--info .navds-alert__icon {
|
||||||
|
color: var(--a-icon-action);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-alert--success {
|
||||||
|
background-color: var(--a-surface-success-moderate);
|
||||||
|
border-color: var(--a-icon-success);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-alert--success .navds-alert__icon {
|
||||||
|
color: var(--a-icon-success);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-alert--warning {
|
||||||
|
background-color: var(--a-surface-warning-moderate);
|
||||||
|
border-color: var(--a-icon-warning);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-alert--warning .navds-alert__icon {
|
||||||
|
color: var(--a-icon-warning);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-alert--error {
|
||||||
|
background-color: var(--a-surface-error-moderate);
|
||||||
|
border-color: var(--a-icon-danger);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-alert--error .navds-alert__icon {
|
||||||
|
color: var(--a-icon-danger);
|
||||||
|
}
|
||||||
@@ -2,3 +2,5 @@
|
|||||||
@import "./tokens-layer.css";
|
@import "./tokens-layer.css";
|
||||||
@import "./typography.css";
|
@import "./typography.css";
|
||||||
@import "./button.css";
|
@import "./button.css";
|
||||||
|
@import "./textfield.css";
|
||||||
|
@import "./alert.css";
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
/**
|
||||||
|
* TextField and shared form-field styles
|
||||||
|
*/
|
||||||
|
|
||||||
|
.navds-form-field {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: var(--a-spacing-2);
|
||||||
|
font-family: var(--a-font-family);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-form-field__label {
|
||||||
|
font-size: var(--a-font-size-md);
|
||||||
|
font-weight: var(--a-font-weight-bold);
|
||||||
|
color: var(--a-text-default);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-form-field__description {
|
||||||
|
margin: 0;
|
||||||
|
font-size: var(--a-font-size-xs);
|
||||||
|
color: var(--a-text-subtle);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-text-field {
|
||||||
|
padding: var(--a-spacing-2) var(--a-spacing-3);
|
||||||
|
border: 1px solid var(--a-border-default);
|
||||||
|
border-radius: var(--a-border-radius-medium);
|
||||||
|
background-color: var(--a-bg-default);
|
||||||
|
color: var(--a-text-default);
|
||||||
|
font-family: inherit;
|
||||||
|
font-size: var(--a-font-size-md);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-form-field--small .navds-text-field {
|
||||||
|
padding: var(--a-spacing-1) var(--a-spacing-2);
|
||||||
|
font-size: var(--a-font-size-xs);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-text-field:hover {
|
||||||
|
border-color: var(--a-border-action);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-text-field:focus-visible {
|
||||||
|
outline: 2px solid var(--a-border-focus);
|
||||||
|
outline-offset: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-text-field--error,
|
||||||
|
.navds-text-field--error:hover {
|
||||||
|
border-color: var(--a-border-danger);
|
||||||
|
box-shadow: 0 0 0 1px var(--a-border-danger);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-form-field__error {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: var(--a-spacing-1);
|
||||||
|
margin: 0;
|
||||||
|
font-size: var(--a-font-size-xs);
|
||||||
|
font-weight: var(--a-font-weight-bold);
|
||||||
|
color: var(--a-icon-danger);
|
||||||
|
}
|
||||||
|
|
||||||
|
.navds-sr-only {
|
||||||
|
position: absolute;
|
||||||
|
width: 1px;
|
||||||
|
height: 1px;
|
||||||
|
padding: 0;
|
||||||
|
margin: -1px;
|
||||||
|
overflow: hidden;
|
||||||
|
clip: rect(0, 0, 0, 0);
|
||||||
|
white-space: nowrap;
|
||||||
|
border: 0;
|
||||||
|
}
|
||||||
@@ -15,6 +15,9 @@
|
|||||||
"files": [
|
"files": [
|
||||||
"dist"
|
"dist"
|
||||||
],
|
],
|
||||||
|
"dependencies": {
|
||||||
|
"@navikt/aksel-icons": "workspace:*"
|
||||||
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc -p tsconfig.json",
|
"build": "tsc -p tsconfig.json",
|
||||||
"clean": "rm -rf dist"
|
"clean": "rm -rf dist"
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
import { forwardRef } from "react";
|
||||||
|
import type { HTMLAttributes, ReactNode } from "react";
|
||||||
|
import {
|
||||||
|
CheckmarkIcon,
|
||||||
|
InformationIcon,
|
||||||
|
WarningIcon,
|
||||||
|
XMarkIcon,
|
||||||
|
} from "@navikt/aksel-icons";
|
||||||
|
import { cn } from "../util/cn";
|
||||||
|
|
||||||
|
export interface AlertProps extends HTMLAttributes<HTMLDivElement> {
|
||||||
|
/**
|
||||||
|
* Alert variant.
|
||||||
|
* @default "info"
|
||||||
|
*/
|
||||||
|
variant?: "info" | "success" | "warning" | "error";
|
||||||
|
/**
|
||||||
|
* Alert size.
|
||||||
|
* @default "medium"
|
||||||
|
*/
|
||||||
|
size?: "medium" | "small";
|
||||||
|
/**
|
||||||
|
* Stretches the alert to fill its container.
|
||||||
|
* @default false
|
||||||
|
*/
|
||||||
|
fullWidth?: boolean;
|
||||||
|
children: ReactNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
const variantIcons = {
|
||||||
|
info: InformationIcon,
|
||||||
|
success: CheckmarkIcon,
|
||||||
|
warning: WarningIcon,
|
||||||
|
error: XMarkIcon,
|
||||||
|
} as const;
|
||||||
|
|
||||||
|
export const Alert = forwardRef<HTMLDivElement, AlertProps>(function Alert(
|
||||||
|
{ variant = "info", size = "medium", fullWidth = false, className, children, ...rest },
|
||||||
|
ref,
|
||||||
|
) {
|
||||||
|
const Icon = variantIcons[variant];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div
|
||||||
|
ref={ref}
|
||||||
|
role={variant === "error" ? "alert" : "status"}
|
||||||
|
className={cn(
|
||||||
|
"navds-alert",
|
||||||
|
`navds-alert--${variant}`,
|
||||||
|
`navds-alert--${size}`,
|
||||||
|
fullWidth && "navds-alert--full-width",
|
||||||
|
className,
|
||||||
|
)}
|
||||||
|
{...rest}
|
||||||
|
>
|
||||||
|
<Icon className="navds-alert__icon" />
|
||||||
|
<div className="navds-alert__content">{children}</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
export { Alert } from "./Alert";
|
||||||
|
export type { AlertProps } from "./Alert";
|
||||||
@@ -4,3 +4,7 @@ export { BodyShort } from "./body-short";
|
|||||||
export type { BodyShortProps } from "./body-short";
|
export type { BodyShortProps } from "./body-short";
|
||||||
export { Heading } from "./heading";
|
export { Heading } from "./heading";
|
||||||
export type { HeadingProps } from "./heading";
|
export type { HeadingProps } from "./heading";
|
||||||
|
export { TextField } from "./text-field";
|
||||||
|
export type { TextFieldProps } from "./text-field";
|
||||||
|
export { Alert } from "./alert";
|
||||||
|
export type { AlertProps } from "./alert";
|
||||||
@@ -0,0 +1,66 @@
|
|||||||
|
import { forwardRef, useId } from "react";
|
||||||
|
import type { InputHTMLAttributes, ReactNode } from "react";
|
||||||
|
import { cn } from "../util/cn";
|
||||||
|
|
||||||
|
export interface TextFieldProps
|
||||||
|
extends Omit<InputHTMLAttributes<HTMLInputElement>, "size"> {
|
||||||
|
/** Field label. */
|
||||||
|
label: ReactNode;
|
||||||
|
/** Additional description shown below the label. */
|
||||||
|
description?: ReactNode;
|
||||||
|
/** Error message. When set, the field is marked invalid. */
|
||||||
|
error?: ReactNode;
|
||||||
|
/**
|
||||||
|
* Input size.
|
||||||
|
* @default "medium"
|
||||||
|
*/
|
||||||
|
size?: "medium" | "small";
|
||||||
|
/**
|
||||||
|
* Visually hides the label.
|
||||||
|
* @default false
|
||||||
|
*/
|
||||||
|
hideLabel?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const TextField = forwardRef<HTMLInputElement, TextFieldProps>(function TextField(
|
||||||
|
{ label, description, error, size = "medium", hideLabel = false, id, className, ...rest },
|
||||||
|
ref,
|
||||||
|
) {
|
||||||
|
const generatedId = useId();
|
||||||
|
const inputId = id ?? generatedId;
|
||||||
|
const errorId = `${inputId}-error`;
|
||||||
|
const descriptionId = `${inputId}-description`;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className={cn("navds-form-field", `navds-form-field--${size}`, className)}>
|
||||||
|
<label
|
||||||
|
className={cn("navds-form-field__label", hideLabel && "navds-sr-only")}
|
||||||
|
htmlFor={inputId}
|
||||||
|
>
|
||||||
|
{label}
|
||||||
|
</label>
|
||||||
|
{description ? (
|
||||||
|
<p className="navds-form-field__description" id={descriptionId}>
|
||||||
|
{description}
|
||||||
|
</p>
|
||||||
|
) : null}
|
||||||
|
<input
|
||||||
|
ref={ref}
|
||||||
|
id={inputId}
|
||||||
|
className={cn("navds-text-field", error && "navds-text-field--error")}
|
||||||
|
aria-invalid={error ? true : undefined}
|
||||||
|
aria-describedby={
|
||||||
|
[description ? descriptionId : undefined, error ? errorId : undefined]
|
||||||
|
.filter(Boolean)
|
||||||
|
.join(" ") || undefined
|
||||||
|
}
|
||||||
|
{...rest}
|
||||||
|
/>
|
||||||
|
{error ? (
|
||||||
|
<p className="navds-form-field__error" id={errorId} role="alert">
|
||||||
|
{error}
|
||||||
|
</p>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
});
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
export { TextField } from "./TextField";
|
||||||
|
export type { TextFieldProps } from "./TextField";
|
||||||
Reference in new issue
Block a user