✨ Add Chip component
Chip comes in neutral and action variants and supports a removable state with a delete button. Includes styles and barrel exports.
This commit is contained in:
1 parent
f9489fdffc
commit
df47b69155
5 files changed
+105
No files matched your search
@@ -0,0 +1,50 @@
|
||||
import { forwardRef } from "react";
|
||||
import type { HTMLAttributes, MouseEventHandler, ReactNode } from "react";
|
||||
import { XMarkIcon } from "@navikt/aksel-icons";
|
||||
import { cn } from "../util/cn";
|
||||
|
||||
export interface ChipProps extends HTMLAttributes<HTMLSpanElement> {
|
||||
/**
|
||||
* Chip variant.
|
||||
* @default "neutral"
|
||||
*/
|
||||
variant?: "neutral" | "action";
|
||||
/**
|
||||
* Renders a delete button next to the label.
|
||||
* @default false
|
||||
*/
|
||||
removable?: boolean;
|
||||
/** Called when the delete button is clicked. Requires `removable`. */
|
||||
onDelete?: MouseEventHandler<HTMLButtonElement>;
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export const Chip = forwardRef<HTMLSpanElement, ChipProps>(function Chip(
|
||||
{ variant = "neutral", removable = false, onDelete, className, children, ...rest },
|
||||
ref,
|
||||
) {
|
||||
return (
|
||||
<span
|
||||
ref={ref}
|
||||
className={cn(
|
||||
"navds-chip",
|
||||
`navds-chip--${variant}`,
|
||||
removable && "navds-chip--removable",
|
||||
className,
|
||||
)}
|
||||
{...rest}
|
||||
>
|
||||
<span className="navds-chip__label">{children}</span>
|
||||
{removable ? (
|
||||
<button
|
||||
type="button"
|
||||
className="navds-chip__delete"
|
||||
aria-label="Slett"
|
||||
onClick={onDelete}
|
||||
>
|
||||
<XMarkIcon />
|
||||
</button>
|
||||
) : null}
|
||||
</span>
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,2 @@
|
||||
export { Chip } from "./Chip";
|
||||
export type { ChipProps } from "./Chip";
|
||||
Reference in new issue
Block a user