mirror of https://github.com/portainer/portainer
feat(ui): add icon to button [EE-3662] (#7204)
parent
b4acbfc9e1
commit
88c4a43a19
|
@ -2,6 +2,7 @@
|
||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
display: inline-flex;
|
display: inline-flex;
|
||||||
justify-content: space-around;
|
justify-content: space-around;
|
||||||
|
align-items: center;
|
||||||
gap: 5px;
|
gap: 5px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,31 +12,32 @@ pr-icon {
|
||||||
.icon {
|
.icon {
|
||||||
color: currentColor;
|
color: currentColor;
|
||||||
margin: 0;
|
margin: 0;
|
||||||
|
|
||||||
|
font-size: var(--icon-size);
|
||||||
|
height: var(--icon-size);
|
||||||
|
width: var(--icon-size);
|
||||||
|
|
||||||
|
--icon-size: 1em;
|
||||||
}
|
}
|
||||||
|
|
||||||
.icon-xs {
|
.icon-xs {
|
||||||
height: 10px;
|
--icon-size: 10px;
|
||||||
width: 10px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.icon-sm {
|
.icon-sm {
|
||||||
height: 14px;
|
--icon-size: 14px;
|
||||||
width: 14px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.icon-md {
|
.icon-md {
|
||||||
height: 16px;
|
--icon-size: 16px;
|
||||||
width: 16px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.icon-lg {
|
.icon-lg {
|
||||||
height: 22px;
|
--icon-size: 22px;
|
||||||
width: 22px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.icon-xl {
|
.icon-xl {
|
||||||
height: 26px;
|
--icon-size: 26px;
|
||||||
width: 26px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.icon.icon-alt {
|
.icon.icon-alt {
|
||||||
|
|
|
@ -9,7 +9,7 @@ export interface IconProps {
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
icon: ReactNode | ComponentType<unknown>;
|
icon: ReactNode | ComponentType<{ size?: string | number }>;
|
||||||
feather?: boolean;
|
feather?: boolean;
|
||||||
className?: string;
|
className?: string;
|
||||||
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl';
|
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl';
|
||||||
|
@ -34,16 +34,6 @@ export function Icon({ icon, feather, className, mode, size }: Props) {
|
||||||
}
|
}
|
||||||
}, [feather]);
|
}, [feather]);
|
||||||
|
|
||||||
if (typeof icon !== 'string') {
|
|
||||||
const Icon = isValidElementType(icon) ? icon : null;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<span className={className} aria-hidden="true" role="img">
|
|
||||||
{Icon == null ? <>{icon}</> : <Icon />}
|
|
||||||
</span>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
const classes = clsx(
|
const classes = clsx(
|
||||||
className,
|
className,
|
||||||
'icon',
|
'icon',
|
||||||
|
@ -51,6 +41,16 @@ export function Icon({ icon, feather, className, mode, size }: Props) {
|
||||||
{ [`icon-${size}`]: size }
|
{ [`icon-${size}`]: size }
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (typeof icon !== 'string') {
|
||||||
|
const Icon = isValidElementType(icon) ? icon : null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<span className={classes} aria-hidden="true" role="img">
|
||||||
|
{Icon == null ? <>{icon}</> : <Icon size="1em" />}
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (feather) {
|
if (feather) {
|
||||||
return (
|
return (
|
||||||
<i
|
<i
|
||||||
|
@ -63,6 +63,6 @@ export function Icon({ icon, feather, className, mode, size }: Props) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<i className={clsx('fa', icon, className)} aria-hidden="true" role="img" />
|
<i className={clsx('fa', icon, classes)} aria-hidden="true" role="img" />
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import { Meta, Story } from '@storybook/react';
|
import { Meta, Story } from '@storybook/react';
|
||||||
import { PropsWithChildren } from 'react';
|
import { PropsWithChildren } from 'react';
|
||||||
|
import { Download } from 'react-feather';
|
||||||
|
|
||||||
import { Button, Props } from './Button';
|
import { Button, Props } from './Button';
|
||||||
|
|
||||||
|
@ -16,7 +17,7 @@ function Template({
|
||||||
}: JSX.IntrinsicAttributes & PropsWithChildren<Props>) {
|
}: JSX.IntrinsicAttributes & PropsWithChildren<Props>) {
|
||||||
return (
|
return (
|
||||||
<Button onClick={onClick} color={color} size={size} disabled={disabled}>
|
<Button onClick={onClick} color={color} size={size} disabled={disabled}>
|
||||||
<i className="fa fa-download" aria-hidden="true" /> Primary Button
|
Primary Button
|
||||||
</Button>
|
</Button>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -63,11 +64,42 @@ export function Danger() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function ButtonIcon() {
|
||||||
|
return (
|
||||||
|
<Button color="primary" onClick={() => {}} icon={Download}>
|
||||||
|
Button with an icon
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ButtonIconLarge() {
|
||||||
|
return (
|
||||||
|
<Button color="primary" onClick={() => {}} icon={Download} size="large">
|
||||||
|
Button with an icon
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ButtonIconMedium() {
|
||||||
|
return (
|
||||||
|
<Button color="primary" onClick={() => {}} icon={Download} size="medium">
|
||||||
|
Button with an icon
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function ButtonIconXSmall() {
|
||||||
|
return (
|
||||||
|
<Button color="primary" onClick={() => {}} icon={Download} size="xsmall">
|
||||||
|
Button with an icon
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export function Default() {
|
export function Default() {
|
||||||
return (
|
return (
|
||||||
<Button color="default" onClick={() => {}}>
|
<Button color="default" onClick={() => {}}>
|
||||||
<i className="fa fa-plus-circle" aria-hidden="true" /> Add an environment
|
Default
|
||||||
variable
|
|
||||||
</Button>
|
</Button>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,16 @@
|
||||||
import { AriaAttributes, MouseEventHandler, PropsWithChildren } from 'react';
|
import {
|
||||||
|
AriaAttributes,
|
||||||
|
ComponentType,
|
||||||
|
MouseEventHandler,
|
||||||
|
PropsWithChildren,
|
||||||
|
ReactNode,
|
||||||
|
} from 'react';
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
|
|
||||||
import { AutomationTestingProps } from '@/types';
|
import { AutomationTestingProps } from '@/types';
|
||||||
|
|
||||||
|
import { Icon } from '@@/Icon';
|
||||||
|
|
||||||
type Type = 'submit' | 'button' | 'reset';
|
type Type = 'submit' | 'button' | 'reset';
|
||||||
type Color =
|
type Color =
|
||||||
| 'default'
|
| 'default'
|
||||||
|
@ -17,6 +25,9 @@ type Color =
|
||||||
type Size = 'xsmall' | 'small' | 'medium' | 'large';
|
type Size = 'xsmall' | 'small' | 'medium' | 'large';
|
||||||
|
|
||||||
export interface Props extends AriaAttributes, AutomationTestingProps {
|
export interface Props extends AriaAttributes, AutomationTestingProps {
|
||||||
|
icon?: ReactNode | ComponentType<unknown>;
|
||||||
|
featherIcon?: boolean;
|
||||||
|
|
||||||
color?: Color;
|
color?: Color;
|
||||||
size?: Size;
|
size?: Size;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
|
@ -34,7 +45,10 @@ export function Button({
|
||||||
className,
|
className,
|
||||||
onClick,
|
onClick,
|
||||||
title,
|
title,
|
||||||
|
icon,
|
||||||
|
featherIcon,
|
||||||
children,
|
children,
|
||||||
|
|
||||||
...ariaProps
|
...ariaProps
|
||||||
}: PropsWithChildren<Props>) {
|
}: PropsWithChildren<Props>) {
|
||||||
return (
|
return (
|
||||||
|
@ -42,17 +56,46 @@ export function Button({
|
||||||
/* eslint-disable-next-line react/button-has-type */
|
/* eslint-disable-next-line react/button-has-type */
|
||||||
type={type}
|
type={type}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
className={clsx('btn', `btn-${color}`, sizeClass(size), className)}
|
className={clsx(
|
||||||
|
{
|
||||||
|
'opacity-60': disabled,
|
||||||
|
},
|
||||||
|
`btn btn-${color}`,
|
||||||
|
sizeClass(size),
|
||||||
|
className
|
||||||
|
)}
|
||||||
onClick={onClick}
|
onClick={onClick}
|
||||||
title={title}
|
title={title}
|
||||||
// eslint-disable-next-line react/jsx-props-no-spreading
|
// eslint-disable-next-line react/jsx-props-no-spreading
|
||||||
{...ariaProps}
|
{...ariaProps}
|
||||||
>
|
>
|
||||||
|
{icon && (
|
||||||
|
<Icon
|
||||||
|
icon={icon}
|
||||||
|
size={getIconSize(size)}
|
||||||
|
className="inline-flex"
|
||||||
|
feather={featherIcon}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
{children}
|
{children}
|
||||||
</button>
|
</button>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getIconSize(size: Size) {
|
||||||
|
switch (size) {
|
||||||
|
case 'xsmall':
|
||||||
|
return 'xs';
|
||||||
|
case 'medium':
|
||||||
|
return 'md';
|
||||||
|
case 'large':
|
||||||
|
return 'lg';
|
||||||
|
case 'small':
|
||||||
|
default:
|
||||||
|
return 'sm';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function sizeClass(size?: Size) {
|
function sizeClass(size?: Size) {
|
||||||
switch (size) {
|
switch (size) {
|
||||||
case 'large':
|
case 'large':
|
||||||
|
|
Loading…
Reference in New Issue