mirror of
https://github.com/outline/outline.git
synced 2026-12-06 11:12:32 +03:00
183 lines
4.5 KiB
TypeScript
183 lines
4.5 KiB
TypeScript
import type { Transition } from "framer-motion";
|
|
import { m } from "framer-motion";
|
|
import type { LocationDescriptor } from "history";
|
|
import { isEqual } from "es-toolkit/compat";
|
|
import queryString from "query-string";
|
|
import * as React from "react";
|
|
import styled, { css, useTheme } from "styled-components";
|
|
import { breakpoint } from "@shared/styles/breakpoint";
|
|
import { MOBILE_TOUCH_TARGET } from "@shared/constants";
|
|
import { s, hover } from "@shared/styles";
|
|
import NavLink from "~/components/NavLink";
|
|
|
|
interface BaseProps {
|
|
/**
|
|
* If true, the tab will only be active if the path matches exactly.
|
|
*/
|
|
exact?: boolean;
|
|
/**
|
|
* If true, the tab will only be active if the query string matches exactly.
|
|
* By default query string parameters are ignored for location matching.
|
|
*/
|
|
exactQueryString?: boolean;
|
|
children?: React.ReactNode;
|
|
/** Ref to the underlying button, only attached in button mode. */
|
|
ref?: React.Ref<HTMLButtonElement>;
|
|
}
|
|
|
|
interface LinkProps extends BaseProps {
|
|
/**
|
|
* The path to match against the current location.
|
|
*/
|
|
to: LocationDescriptor;
|
|
/**
|
|
* Optional click handler called when the tab is clicked (in addition to navigation).
|
|
*/
|
|
onClick?: () => void;
|
|
active?: never;
|
|
}
|
|
|
|
interface ButtonProps
|
|
extends BaseProps, React.ButtonHTMLAttributes<HTMLButtonElement> {
|
|
/**
|
|
* Click handler for button mode, optional when the parent handles selection.
|
|
*/
|
|
onClick?: React.MouseEventHandler<HTMLButtonElement>;
|
|
/**
|
|
* Whether the tab is currently active (only used in button mode).
|
|
*/
|
|
active: boolean;
|
|
to?: never;
|
|
}
|
|
|
|
type Props = LinkProps | ButtonProps;
|
|
|
|
const tabStyles = css`
|
|
position: relative;
|
|
display: inline-flex;
|
|
align-items: center;
|
|
font-weight: 500;
|
|
font-size: 14px;
|
|
cursor: var(--pointer);
|
|
user-select: none;
|
|
padding: 12px 0;
|
|
min-height: ${MOBILE_TOUCH_TARGET}px;
|
|
|
|
${breakpoint("tablet")`
|
|
padding: 6px 0;
|
|
min-height: 0;
|
|
`};
|
|
`;
|
|
|
|
const TabLink = styled(NavLink)`
|
|
${tabStyles}
|
|
color: ${s("textTertiary")};
|
|
|
|
&: ${hover} {
|
|
color: ${s("textSecondary")};
|
|
}
|
|
`;
|
|
|
|
const TabButton = styled.button<{ $active: boolean }>`
|
|
${tabStyles}
|
|
color: ${({ $active }) => ($active ? s("textSecondary") : s("textTertiary"))};
|
|
background: none;
|
|
border: none;
|
|
|
|
&: ${hover} {
|
|
color: ${s("textSecondary")};
|
|
}
|
|
`;
|
|
|
|
const Active = styled(m.div)`
|
|
position: absolute;
|
|
bottom: 0;
|
|
left: 0;
|
|
right: 0;
|
|
height: 3px;
|
|
width: 100%;
|
|
border-radius: 3px;
|
|
background: ${s("textSecondary")};
|
|
`;
|
|
|
|
const transition: Transition = {
|
|
type: "spring",
|
|
stiffness: 500,
|
|
damping: 30,
|
|
};
|
|
|
|
/** Restrict shared layout animation to the X axis only. */
|
|
const horizontalOnly = (transform: Record<string, string>, generated: string) =>
|
|
generated.replace(
|
|
/translate3d\(([^,]+),\s*[^,]+,\s*([^)]+)\)/,
|
|
"translate3d($1, 0px, $2)"
|
|
);
|
|
|
|
/**
|
|
* A single tab with an animated active underline, rendered either as a link
|
|
* matched against the current location, or as a button in controlled mode.
|
|
* A forwarded ref is only attached in button mode.
|
|
*/
|
|
export function Tab(props: Props) {
|
|
const { children, exact, exactQueryString } = props;
|
|
const theme = useTheme();
|
|
const activeStyle = {
|
|
color: theme.textSecondary,
|
|
};
|
|
|
|
// Button mode - controlled by onClick and active props (no `to` prop)
|
|
if ("active" in props && !("to" in props)) {
|
|
const {
|
|
ref,
|
|
active,
|
|
exact: _exact,
|
|
exactQueryString: _exactQueryString,
|
|
...rest
|
|
} = props;
|
|
|
|
return (
|
|
<TabButton {...rest} ref={ref} type="button" $active={active}>
|
|
{children}
|
|
{active && (
|
|
<Active
|
|
layoutId="underline"
|
|
initial={false}
|
|
transition={transition}
|
|
transformTemplate={horizontalOnly}
|
|
/>
|
|
)}
|
|
</TabButton>
|
|
);
|
|
}
|
|
|
|
// Link mode - controlled by react-router
|
|
const { to, ref: _ref, ...rest } = props as LinkProps;
|
|
return (
|
|
<TabLink
|
|
{...rest}
|
|
to={to}
|
|
exact={exact || exactQueryString}
|
|
activeStyle={activeStyle}
|
|
>
|
|
{(match, location) => (
|
|
<>
|
|
{children}
|
|
{match &&
|
|
(!exactQueryString ||
|
|
isEqual(
|
|
queryString.parse(location.search ?? ""),
|
|
queryString.parse(to.search as string)
|
|
)) && (
|
|
<Active
|
|
layoutId="underline"
|
|
initial={false}
|
|
transition={transition}
|
|
transformTemplate={horizontalOnly}
|
|
/>
|
|
)}
|
|
</>
|
|
)}
|
|
</TabLink>
|
|
);
|
|
}
|