@amaui/ui-react
Version:
UI for React
155 lines • 6.57 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
const _excluded = ["tonal", "color", "version", "size", "value", "onChange", "active", "index", "name", "label", "icon", "iconPosition", "activateOnFocus", "disabled", "onBlur", "onFocus", "LineProps", "Component", "className", "children"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import React from 'react';
import { is } from '@amaui/utils';
import { classNames, style as styleMethod, useAmauiTheme } from '@amaui/style-react';
import TypeElement from '../Type';
import SurfaceElement from '../Surface';
import LineElement from '../Line';
import InteractionElement from '../Interaction';
import { staticClassName } from '../utils';
const useStyle = styleMethod(theme => ({
root: {
// Reset
fontFamily: 'inherit',
'-webkit-appearance': 'none',
appearance: 'none',
background: 'transparent',
border: 'none',
fontSize: '100%',
lineHeight: '1.15',
margin: '0',
overflow: 'visible',
textTransform: 'none',
position: 'relative',
flex: '0 0 auto',
alignSelf: 'stretch',
userSelect: 'none',
cursor: 'pointer',
opacity: theme.palette.visual_contrast.default.opacity.secondary,
transition: theme.methods.transitions.make('opacity')
},
size_small: {
padding: `${theme.methods.space.value(1.5, 'px')} ${theme.methods.space.value(3, 'px')}`,
minWidth: '40px',
maxWidth: '200px'
},
size_regular: {
padding: `${theme.methods.space.value(2, 'px')} ${theme.methods.space.value(4, 'px')}`,
minWidth: '70px',
maxWidth: '240px'
},
size_large: {
padding: `${theme.methods.space.value(2.5, 'px')} ${theme.methods.space.value(5, 'px')}`,
minWidth: '100px',
maxWidth: '280px'
},
type_small: {
maxHeight: '2rem'
},
type_regular: {
maxHeight: '2.15rem'
},
type_large: {
maxHeight: '2.6rem'
},
line: {
overflow: 'hidden'
},
active: {
opacity: '1'
},
disabled: {
pointerEvents: 'none',
opacity: theme.palette.visual_contrast.default.opacity.disabled,
cursor: 'default'
}
}), {
name: 'amaui-Tab'
});
const Tab = /*#__PURE__*/React.forwardRef((props_, ref) => {
const theme = useAmauiTheme();
const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.amauiTab?.props?.default), props_), [props_]);
const Line = React.useMemo(() => theme?.elements?.Line || LineElement, [theme]);
const Type = React.useMemo(() => theme?.elements?.Type || TypeElement, [theme]);
const Surface = React.useMemo(() => theme?.elements?.Surface || SurfaceElement, [theme]);
const Interaction = React.useMemo(() => theme?.elements?.Interaction || InteractionElement, [theme]);
const {
tonal = true,
color = 'primary',
version = 'primary',
size = 'regular',
value,
onChange,
active,
index,
name,
label: label_,
icon,
iconPosition = 'start',
activateOnFocus,
disabled,
onBlur: onBlur_,
onFocus: onFocus_,
LineProps = {},
Component = 'button',
className,
children
} = props,
other = _objectWithoutProperties(props, _excluded);
const {
classes
} = useStyle();
const [focus, setFocus] = React.useState(false);
const label = name !== undefined ? name : label_;
const onBlur = React.useCallback(event => {
if (!disabled) setFocus(false);
if (is('function', onBlur_)) onBlur_(event);
}, [disabled, onBlur_]);
const onFocus = React.useCallback(event => {
if (!disabled) {
setFocus(true);
if (activateOnFocus) onChange(value, index);
}
if (is('function', onFocus_)) onFocus_(event);
}, [activateOnFocus, value, disabled, onFocus_]);
if (icon !== undefined) {
LineProps.direction = iconPosition === 'start' ? 'row' : iconPosition === 'top' ? 'column' : iconPosition === 'end' ? 'row-reverse' : 'column-reverse';
}
let typeVersion = 'l2';
if (size === 'large') typeVersion = 'l1';else if (size === 'small') typeVersion = 'l3';
return /*#__PURE__*/React.createElement(Surface, _extends({
ref: ref,
tabIndex: !disabled ? 0 : -1,
tonal: tonal,
color: color,
onBlur: onBlur,
onFocus: onFocus,
role: "tab",
"aria-selected": active,
"data-amaui-tab-value": value,
Component: Component,
className: classNames([staticClassName('Tab', theme) && ['amaui-Tab-root', `amaui-Tab-version-${version}`, `amaui-Tab-size-${size}`, active && `amaui-Tab-active`, disabled && `amaui-Tab-disabled`], className, classes.root, classes[`size_${size}`], active && classes.active, disabled && classes.disabled])
}, other), /*#__PURE__*/React.createElement(Interaction, {
pulse: focus
}), /*#__PURE__*/React.createElement(Line, _extends({
gap: 1,
direction: "row",
align: "center",
justify: "center"
}, LineProps, {
className: classNames([staticClassName('Tab', theme) && ['amaui-Tab-line'], LineProps?.className, classes.line])
}), icon, label !== undefined && (is('simple', label) ? /*#__PURE__*/React.createElement(Type, {
version: typeVersion,
className: classNames([staticClassName('Tab', theme) && ['amaui-Tab-type'], classes[`type_${size}`]])
}, label) : label), children !== undefined && (is('simple', children) ? /*#__PURE__*/React.createElement(Type, {
version: typeVersion,
className: classNames([staticClassName('Tab', theme) && ['amaui-Tab-type'], classes[`type_${size}`]])
}, children) : children)));
});
Tab.displayName = 'amaui-Tab';
export default Tab;