import { styled, View } from '@hanzo/gui';
import * as React from 'react';
import { Skeleton } from './skeleton';
// A button whose whole content is an icon.
//
// `expanded`, `selected` and `highlighted` are React props, so the look they
// select is chosen HERE, by merging style objects, rather than by writing
// `data-[selected]:` classes and letting a stylesheet re-derive the state that
// this component already knows. The `data-*` attributes are still emitted —
// they are a DOM contract consumers target — but nothing in this file styles
// through them.
//
// Two things stay CSS because no prop reaches them: the spinner's keyframe (a
// gui transition interpolates between two states, it does not loop) and the
// size of a descendant `svg`.
const IconButtonFrame = styled(View, {
name: 'LuxIconButton',
render: ,
role: 'button',
cursor: 'pointer',
display: 'inline-flex',
alignItems: 'center',
justifyContent: 'center',
padding: 0,
minWidth: 'auto' as never,
flexShrink: 1,
backgroundColor: 'transparent',
borderWidth: 0,
variants: {
size: {
'2xs': { width: 20, height: 20, borderRadius: 4 },
'2xs_alt': { width: 20, height: 20, borderRadius: 4 },
md: { width: 32, height: 32, borderRadius: 8 },
},
disabled: {
true: { opacity: 0.4, cursor: 'not-allowed', pointerEvents: 'none' },
},
} as const,
});
type Style = Record;
const HOVER = 'var(--color-hover)';
const SELECTED_BG = 'var(--color-selected-control-bg)';
const SELECTED_FG = 'var(--color-selected-control-text)';
/**
* Resting look, and the looks that replace it while selected or expanded.
* Merged in that order, so a key set in two of them resolves to the later —
* which is why `selected` and `expanded` are objects rather than `hoverStyle`
* entries that would fight the resting value.
*/
const VARIANT: Record = {
plain: {
rest: { backgroundColor: 'transparent', hoverStyle: { backgroundColor: 'transparent' } },
},
icon_secondary: {
rest: {
backgroundColor: 'transparent',
color: 'var(--color-icon-secondary)',
hoverStyle: { color: HOVER },
},
selected: { backgroundColor: SELECTED_BG, color: SELECTED_FG, hoverStyle: { color: HOVER } },
expanded: { color: HOVER },
},
icon_background: {
rest: {
backgroundColor: 'var(--color-button-icon-background-bg)',
color: 'var(--color-icon-secondary)',
hoverStyle: { color: HOVER },
},
selected: { backgroundColor: SELECTED_BG, color: SELECTED_FG, hoverStyle: { color: HOVER } },
expanded: { color: HOVER },
},
link: {
rest: {
backgroundColor: 'transparent',
color: 'var(--color-link-primary)',
fontWeight: '400',
paddingHorizontal: 0,
height: 'auto',
hoverStyle: { backgroundColor: 'transparent', color: 'var(--color-link-primary-hover)' },
disabledStyle: { color: 'var(--color-text-secondary)' },
},
},
dropdown: {
rest: {
borderWidth: 2,
borderStyle: 'solid',
backgroundColor: 'transparent',
color: 'var(--color-button-dropdown-fg)',
borderColor: 'var(--color-button-dropdown-border)',
hoverStyle: { backgroundColor: 'transparent', color: HOVER, borderColor: HOVER },
},
selected: { backgroundColor: SELECTED_BG, color: SELECTED_FG, borderColor: 'transparent', hoverStyle: { color: HOVER } },
expanded: { backgroundColor: 'transparent', color: HOVER, borderColor: HOVER },
},
pagination: {
rest: {
borderWidth: 2,
borderStyle: 'solid',
backgroundColor: 'transparent',
color: 'var(--color-button-pagination-fg)',
borderColor: 'var(--color-button-pagination-border)',
hoverStyle: { backgroundColor: 'transparent', color: HOVER, borderColor: HOVER },
},
selected: { backgroundColor: SELECTED_BG, color: SELECTED_FG, borderColor: 'transparent' },
},
};
/** Descendant `svg` size per control size, published as a custom property. */
const ICON_SIZE: Record = { '2xs': 20, '2xs_alt': 12, md: 20 };
type Variant = keyof typeof VARIANT;
type Size = '2xs' | '2xs_alt' | 'md';
export interface IconButtonProps extends Omit, 'size'> {
/** Visual variant */
variant?: Variant;
/** Icon-button size */
size?: Size;
/** Show a loading spinner (disables button) */
loading?: boolean;
/** Render a skeleton placeholder instead of the button */
loadingSkeleton?: boolean;
/** Also emitted as data-expanded for consumer styling */
expanded?: boolean;
/** Also emitted as data-selected for consumer styling */
selected?: boolean;
/** Also emitted as data-highlighted */
highlighted?: boolean;
/** Polymorphic element type (e.g. "div") */
as?: React.ElementType;
}
export const IconButton = React.forwardRef(
function IconButton(props, ref) {
const {
size, variant = 'plain', loading, loadingSkeleton = false,
expanded, selected, highlighted, disabled, className, children,
as: asProp, style: styleProp, ...rest
} = props;
const look = VARIANT[variant] ?? VARIANT.plain;
const iconSize = size ? ICON_SIZE[size] : undefined;
const button = (
{ loading ? (
}
role="status"
aria-label="Loading"
width={ 20 }
height={ 20 }
borderRadius={ 1000 }
borderWidth={ 2 }
borderColor={ 'currentColor' as never }
borderBottomColor={ 'transparent' as never }
borderLeftColor={ 'transparent' as never }
/>
) : children }
);
if (loadingSkeleton) {
return (
}>
{ button }
);
}
return button;
},
);