Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 33 additions & 12 deletions packages/fuselage/src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AllHTMLAttributes } from 'react';
import { forwardRef, useMemo } from 'react';
import { forwardRef, useMemo, Children } from 'react';

import { Box, type BoxProps } from '../Box';
import { Icon, type IconProps } from '../Icon';
Expand Down Expand Up @@ -39,6 +39,7 @@ const Button = forwardRef<HTMLButtonElement | HTMLAnchorElement, ButtonProps>(
external,
icon,
is = 'button',
type = 'button', // Defaulting to 'button' here for safety
rel: _rel,
tiny,
mini,
Expand All @@ -53,15 +54,20 @@ const Button = forwardRef<HTMLButtonElement | HTMLAnchorElement, ButtonProps>(
},
ref,
) {
const extraProps =
(is === 'a' && {
rel: external ? 'noopener noreferrer' : undefined,
target: external ? '_blank' : undefined,
}) ||
(is === 'button' && {
type: 'button',
}) ||
{};
const extraProps = useMemo(() => {
if (is === 'a') {
return {
rel: external ? 'noopener noreferrer' : undefined,
target: external ? '_blank' : undefined,
};
}

if (is === 'button') {
return { type }; // Uses the 'type' prop (default 'button')
}

return {};
}, [is, external, type]);

const kindAndVariantProps = useMemo(() => {
const variant =
Expand All @@ -83,10 +89,25 @@ const Button = forwardRef<HTMLButtonElement | HTMLAnchorElement, ButtonProps>(
return {};
}, [primary, secondary, danger, warning, success]);

// --- Accessibility Check (Dev Only) ---
if (process.env.NODE_ENV !== 'production') {
const childrenArray = Children.toArray(children);
const hasTextContent = childrenArray.some(
(child) => typeof child === 'string' && child.trim().length > 0
);

const isVisualOnly = !hasTextContent && (!!icon || !!loading || square);

if (isVisualOnly && !props['aria-label'] && !props['aria-labelledby']) {
console.warn(
`Fuselage [Button]: Buttons without visible text (icon-only or square buttons) must provide an 'aria-label' or 'aria-labelledby' prop for accessibility.`
);
}
}

return (
<Box
is={is}
type='button'
rcx-button
{...kindAndVariantProps}
rcx-button--small={small}
Expand Down Expand Up @@ -114,4 +135,4 @@ const Button = forwardRef<HTMLButtonElement | HTMLAnchorElement, ButtonProps>(
},
);

export default Button;
export default Button;