button-component #29

Merged
stne3960 merged 22 commits from button-component into main 2025-12-12 11:52:31 +01:00
Showing only changes of commit 15b50844cb - Show all commits

View File

@ -0,0 +1,57 @@
import type { ButtonHTMLAttributes, ReactNode } from 'react';
export type ButtonVariant = 'primary' | 'secondary' | 'red' | 'green';
export type ButtonSize = 'sm' | 'md' | 'lg';
export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
variant?: ButtonVariant;
size?: ButtonSize;
children: ReactNode;
}
const variantClasses: Record<ButtonVariant, string> = {
primary: 'bg-primary text-base-canvas border border-primary hover:bg-secondary hover:text-base-canvas hover:border-primary focus:bg-base-canvas focus:text-base-ink-strong focus:border-primary focus:outline focus:outline-sky-100',
secondary: 'bg-base-canvas text-base-ink-strong border border-base-ink-soft hover:bg-base-canvas hover:text-base-ink-strong hover:border-base-ink-medium focus:bg-base-canvas focus:text-base-ink-strong focus:border-primary focus:outline focus:outline-sky-100',
stne3960 marked this conversation as resolved Outdated

This is unwieldy. Any way to break it down or group them somehow? Like the border styles, focus styles, colors, and so on.

Is it necessary to repeat the prefix or can you do something like hover:[a, b, c] instead of hover:a hover:b hover:c?

This is unwieldy. Any way to break it down or group them somehow? Like the border styles, focus styles, colors, and so on. Is it necessary to repeat the prefix or can you do something like `hover:[a, b, c]` instead of `hover:a hover:b hover:c`?
red: 'bg-other-red-100 text-su-white border border-other-red-100 focus:bg-base-canvas focus:text-base-ink-strong focus:border-primary focus:outline focus:outline-sky-100',
green: 'bg-other-green text-su-white border border-other-green focus:bg-base-canvas focus:text-base-ink-strong focus:border-primary focus:outline focus:outline-sky-100',
};
const sizeClasses: Record<ButtonSize, string> = {
sm: 'h-(--control-height-sm) min-w-(--button-min-width-sm) px-(--button-padding-x-sm) text-body-md rounded-(--border-radius-sm)',
md: 'h-(--control-height-md) min-w-(--button-min-width-md) px-(--button-padding-x-md) text-body-md rounded-(--border-radius-sm)',
lg: 'h-(--control-height-lg) min-w-(--button-min-width-lg) px-(--button-padding-x-lg) text-body-lg rounded-(--border-radius-md)',
};
const textPaddingClasses: Record<ButtonSize, string> = {
sm: 'px-(--button-text-padding-x-sm)',
md: 'px-(--button-text-padding-x-md)',
lg: 'px-(--button-text-padding-x-lg)',
};
export default function Button({
variant = 'primary',
size = 'md',
className = '',
children,
...props
}: ButtonProps) {
const baseClasses = 'inline-flex items-center justify-center font-semibold';
const classes = [
baseClasses,
variantClasses[variant],
sizeClasses[size],
className,
]
.filter(Boolean)
.join(' ');
return (
stne3960 marked this conversation as resolved Outdated

What does this do? I assume it does some JavaScript type-coercion of strings/undefined/null to booleans but what about the empty string? Needs a clarification comment.

My initial read through of the code, none of the strings in the list should be null or undefined and joining an empty string shouldn't matter much.

What does this do? I assume it does some JavaScript type-coercion of strings/`undefined`/`null` to booleans but what about the empty string? Needs a clarification comment. My initial read through of the code, none of the strings in the list should be `null` or `undefined` and joining an empty string shouldn't matter much.

You're right that the current implementation passes only non-empty strings, so .filter(Boolean) doesn't change behavior today.

The reason it's useful is as soon as someone adds conditional classes or optional props (e.g. isDisabled && "opacity-50 cursor-not-allowed"), it's easy for the array to end up containing false, undefined, or empty strings. Without filtering, those values can end up in the final className and produce a noisy DOM.

Using .filter(Boolean) keeps the class string clean no matter how the component evolves. I'll add a short comment explaining this so future contributors understand the intent.

You're right that the current implementation passes only non-empty strings, so `.filter(Boolean)` doesn't change behavior today. The reason it's useful is as soon as someone adds conditional classes or optional props (e.g. `isDisabled && "opacity-50 cursor-not-allowed"`), it's easy for the array to end up containing false, undefined, or empty strings. Without filtering, those values can end up in the final className and produce a noisy DOM. Using `.filter(Boolean)` keeps the class string clean no matter how the component evolves. I'll add a short comment explaining this so future contributors understand the intent.
<button
className={classes}
{...props}
>
<span className={textPaddingClasses[size]}>{children}</span>
</button>
);
}