mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
234 lines
5.9 KiB
TypeScript
234 lines
5.9 KiB
TypeScript
import type { LocationDescriptor } from "history";
|
|
import { DisclosureIcon } from "outline-icons";
|
|
import { darken, lighten, transparentize } from "polished";
|
|
import * as React from "react";
|
|
import styled from "styled-components";
|
|
import { breakpoint } from "@shared/styles/breakpoint";
|
|
import { MOBILE_TOUCH_TARGET } from "@shared/constants";
|
|
import { s } from "@shared/styles";
|
|
import type { Props as ActionButtonProps } from "~/components/ActionButton";
|
|
import ActionButton from "~/components/ActionButton";
|
|
import { undraggableOnDesktop } from "~/styles";
|
|
|
|
type RealProps = {
|
|
$fullwidth?: boolean;
|
|
$borderOnHover?: boolean;
|
|
$neutral?: boolean;
|
|
$danger?: boolean;
|
|
};
|
|
|
|
const RealButton = styled(ActionButton)<RealProps>`
|
|
display: ${(props) => (props.$fullwidth ? "block" : "inline-block")};
|
|
width: ${(props) => (props.$fullwidth ? "100%" : "auto")};
|
|
margin: 0;
|
|
padding: 0;
|
|
border: 0;
|
|
background: ${s("accent")};
|
|
color: ${s("accentText")};
|
|
box-shadow: rgba(0, 0, 0, 0.2) 0px 1px 2px;
|
|
border-radius: 6px;
|
|
font-size: 14px;
|
|
font-weight: 500;
|
|
height: 32px;
|
|
text-decoration: none;
|
|
flex-shrink: 0;
|
|
cursor: var(--pointer);
|
|
user-select: none;
|
|
appearance: none !important;
|
|
transition: background 200ms ease-out;
|
|
${undraggableOnDesktop()}
|
|
|
|
${breakpoint("mobile", "tablet")`
|
|
height: ${MOBILE_TOUCH_TARGET}px;
|
|
border-radius: ${MOBILE_TOUCH_TARGET / 2}px;
|
|
font-size: 16px;
|
|
`};
|
|
|
|
&::-moz-focus-inner {
|
|
padding: 0;
|
|
border: 0;
|
|
}
|
|
|
|
&:hover:not(:disabled),
|
|
&[aria-expanded="true"] {
|
|
background: ${(props) => darken(0.05, props.theme.accent)};
|
|
transition: background 0s;
|
|
}
|
|
|
|
&:disabled {
|
|
cursor: default;
|
|
pointer-events: none;
|
|
color: ${(props) => transparentize(0.3, props.theme.accentText)};
|
|
background: ${(props) => transparentize(0.1, props.theme.accent)};
|
|
|
|
svg {
|
|
fill: ${(props) => props.theme.white50};
|
|
}
|
|
}
|
|
|
|
${(props) =>
|
|
props.$neutral &&
|
|
`
|
|
background: inherit;
|
|
color: ${props.theme.buttonNeutralText};
|
|
box-shadow: ${
|
|
props.$borderOnHover
|
|
? "none"
|
|
: `rgba(0, 0, 0, 0.07) 0px 1px 2px, ${props.theme.buttonNeutralBorder} 0 0 0 1px inset`
|
|
};
|
|
|
|
&:hover:not(:disabled),
|
|
&[aria-expanded="true"] {
|
|
background: ${
|
|
props.$borderOnHover
|
|
? props.theme.buttonNeutralBackground
|
|
: props.theme.buttonNeutralHoverBackground
|
|
};
|
|
box-shadow: rgba(0, 0, 0, 0.07) 0px 1px 2px, ${
|
|
props.theme.buttonNeutralBorder
|
|
} 0 0 0 1px inset;
|
|
transition: background 0s;
|
|
}
|
|
|
|
&:focus-visible {
|
|
box-shadow: ${`rgba(0, 0, 0, 0.07) 0px 1px 2px, ${props.theme.inputBorderFocused} 0 0 0 1px inset`};
|
|
}
|
|
|
|
&:disabled {
|
|
color: ${props.theme.textTertiary};
|
|
background: none;
|
|
|
|
svg {
|
|
fill: currentColor;
|
|
}
|
|
}
|
|
`}
|
|
|
|
${(props) =>
|
|
props.$danger &&
|
|
`
|
|
background: ${props.theme.danger};
|
|
color: ${props.theme.white};
|
|
|
|
&:hover:not(:disabled),
|
|
&[aria-expanded="true"] {
|
|
background: ${darken(0.05, props.theme.danger)};
|
|
transition: background 0s;
|
|
}
|
|
|
|
&:disabled {
|
|
background: ${lighten(0.05, props.theme.danger)};
|
|
}
|
|
|
|
&:focus-visible {
|
|
outline-color: ${darken(0.2, props.theme.danger)} !important;
|
|
}
|
|
`};
|
|
`;
|
|
|
|
const Label = styled.span<{ hasIcon?: boolean }>`
|
|
overflow: hidden;
|
|
white-space: nowrap;
|
|
text-overflow: ellipsis;
|
|
|
|
${(props) => props.hasIcon && "padding-inline-start: 4px;"};
|
|
`;
|
|
|
|
interface InnerProps {
|
|
disclosure?: boolean;
|
|
hasIcon?: boolean;
|
|
hasText?: boolean;
|
|
}
|
|
|
|
export const Inner = styled.span<InnerProps>`
|
|
display: flex;
|
|
padding: 0 8px;
|
|
padding-inline-end: ${(props) => (props.disclosure ? 2 : 8)}px;
|
|
line-height: ${(props) => (props.hasIcon ? 24 : 32)}px;
|
|
justify-content: center;
|
|
align-items: center;
|
|
min-height: 32px;
|
|
|
|
${(props) => props.hasIcon && props.hasText && "padding-inline-start: 4px;"};
|
|
${(props) => props.hasIcon && !props.hasText && "padding: 0 4px;"};
|
|
|
|
${breakpoint("mobile", "tablet")`
|
|
min-height: ${MOBILE_TOUCH_TARGET}px;
|
|
padding: 0 16px;
|
|
padding-inline-end: ${(props: InnerProps) => (props.disclosure ? 8 : 16)}px;
|
|
${(props: InnerProps) =>
|
|
props.hasIcon && props.hasText && "padding-inline-start: 12px;"};
|
|
${(props: InnerProps) =>
|
|
props.hasIcon &&
|
|
!props.hasText &&
|
|
`padding: 0 ${(MOBILE_TOUCH_TARGET - 24) / 2}px;`};
|
|
`};
|
|
`;
|
|
|
|
export type Props<T> = ActionButtonProps & {
|
|
icon?: React.ReactNode;
|
|
children?: React.ReactNode;
|
|
disclosure?: boolean;
|
|
neutral?: boolean;
|
|
danger?: boolean;
|
|
fullwidth?: boolean;
|
|
as?: T;
|
|
to?: LocationDescriptor;
|
|
borderOnHover?: boolean;
|
|
hideIcon?: boolean;
|
|
href?: string;
|
|
"data-on"?: string;
|
|
"data-event-category"?: string;
|
|
"data-event-action"?: string;
|
|
ref?: React.Ref<HTMLButtonElement>;
|
|
};
|
|
|
|
function Button<T extends React.ElementType = "button">(
|
|
props: Props<T> & React.ComponentPropsWithoutRef<T>
|
|
) {
|
|
const {
|
|
ref,
|
|
type,
|
|
children,
|
|
value,
|
|
disclosure,
|
|
neutral,
|
|
action,
|
|
icon,
|
|
borderOnHover,
|
|
hideIcon,
|
|
fullwidth,
|
|
danger,
|
|
...rest
|
|
} = props;
|
|
const hasText = !!children || value !== undefined;
|
|
const ic = hideIcon ? undefined : (action?.icon ?? icon);
|
|
const hasIcon = ic !== undefined;
|
|
|
|
return (
|
|
<RealButton
|
|
type={type || "button"}
|
|
ref={ref}
|
|
$neutral={neutral}
|
|
action={action}
|
|
$danger={danger}
|
|
$fullwidth={fullwidth}
|
|
$borderOnHover={borderOnHover}
|
|
data-variant={danger ? "danger" : neutral ? "neutral" : "primary"}
|
|
{...rest}
|
|
>
|
|
<Inner hasIcon={hasIcon} hasText={hasText} disclosure={disclosure}>
|
|
{hasIcon && typeof ic !== "function" && ic}
|
|
{hasText && <Label hasIcon={hasIcon}>{children || value}</Label>}
|
|
{disclosure && <StyledDisclosureIcon />}
|
|
</Inner>
|
|
</RealButton>
|
|
);
|
|
}
|
|
|
|
const StyledDisclosureIcon = styled(DisclosureIcon)`
|
|
opacity: 0.8;
|
|
`;
|
|
|
|
export default Button;
|