✨ Add Button, BodyShort and Heading to @navikt/ds-react
forwardRef-based components with navds-* classnames composed via a small local cn() utility. Adds shared tsconfig.base.json and truncate modifier for BodyShort.
This commit is contained in:
1 parent
b4930f754e
commit
559fd2b971
12 files changed
+164
No files matched your search
@@ -0,0 +1,29 @@
|
||||
import { forwardRef } from "react";
|
||||
import type { HTMLAttributes } from "react";
|
||||
import { cn } from "../util/cn";
|
||||
|
||||
export interface BodyShortProps extends HTMLAttributes<HTMLParagraphElement> {
|
||||
/**
|
||||
* Text size.
|
||||
* @default "medium"
|
||||
*/
|
||||
size?: "medium" | "small";
|
||||
/**
|
||||
* Truncates the text to a single line with an ellipsis.
|
||||
* @default false
|
||||
*/
|
||||
truncate?: boolean;
|
||||
}
|
||||
|
||||
export const BodyShort = forwardRef<HTMLParagraphElement, BodyShortProps>(function BodyShort(
|
||||
{ size = "medium", truncate = false, className, ...rest },
|
||||
ref,
|
||||
) {
|
||||
return (
|
||||
<p
|
||||
ref={ref}
|
||||
className={cn("navds-body-short", `navds-body-short--${size}`, truncate && "navds-body-short--truncate", className)}
|
||||
{...rest}
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,2 @@
|
||||
export { BodyShort } from "./BodyShort";
|
||||
export type { BodyShortProps } from "./BodyShort";
|
||||
@@ -0,0 +1,30 @@
|
||||
import { forwardRef } from "react";
|
||||
import type { ButtonHTMLAttributes } from "react";
|
||||
import { cn } from "../util/cn";
|
||||
|
||||
export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
|
||||
/**
|
||||
* Visual variant.
|
||||
* @default "primary"
|
||||
*/
|
||||
variant?: "primary" | "secondary" | "tertiary" | "danger";
|
||||
/**
|
||||
* Button size.
|
||||
* @default "medium"
|
||||
*/
|
||||
size?: "medium" | "small";
|
||||
}
|
||||
|
||||
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(function Button(
|
||||
{ variant = "primary", size = "medium", type = "button", className, ...rest },
|
||||
ref,
|
||||
) {
|
||||
return (
|
||||
<button
|
||||
ref={ref}
|
||||
type={type}
|
||||
className={cn("navds-button", `navds-button--${variant}`, `navds-button--${size}`, className)}
|
||||
{...rest}
|
||||
/>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,2 @@
|
||||
export { Button } from "./Button";
|
||||
export type { ButtonProps } from "./Button";
|
||||
@@ -0,0 +1,27 @@
|
||||
import { createElement, forwardRef } from "react";
|
||||
import type { HTMLAttributes } from "react";
|
||||
import { cn } from "../util/cn";
|
||||
|
||||
export interface HeadingProps extends HTMLAttributes<HTMLHeadingElement> {
|
||||
/**
|
||||
* Visual size, decoupled from the semantic level.
|
||||
* @default "medium"
|
||||
*/
|
||||
size?: "xlarge" | "large" | "medium" | "small" | "xsmall";
|
||||
/**
|
||||
* Semantic heading level (h1–h6).
|
||||
* @default 1
|
||||
*/
|
||||
level?: 1 | 2 | 3 | 4 | 5 | 6;
|
||||
}
|
||||
|
||||
export const Heading = forwardRef<HTMLHeadingElement, HeadingProps>(function Heading(
|
||||
{ size = "medium", level = 1, className, ...rest },
|
||||
ref,
|
||||
) {
|
||||
return createElement(`h${level}`, {
|
||||
ref,
|
||||
className: cn("navds-heading", `navds-heading--${size}`, className),
|
||||
...rest,
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,2 @@
|
||||
export { Heading } from "./Heading";
|
||||
export type { HeadingProps } from "./Heading";
|
||||
@@ -0,0 +1,6 @@
|
||||
export { Button } from "./button";
|
||||
export type { ButtonProps } from "./button";
|
||||
export { BodyShort } from "./body-short";
|
||||
export type { BodyShortProps } from "./body-short";
|
||||
export { Heading } from "./heading";
|
||||
export type { HeadingProps } from "./heading";
|
||||
@@ -0,0 +1,5 @@
|
||||
type ClassValue = string | false | null | undefined;
|
||||
|
||||
export function cn(...classes: ClassValue[]): string {
|
||||
return classes.filter(Boolean).join(" ");
|
||||
}
|
||||
Reference in new issue
Block a user