Files
outline/app/components/Tab.tsx
T
Tom Moor 0139b91b5d chore: Replace lodash with es-toolkit (#12281)
* chore: Replace lodash with es-toolkit

Migrate all direct lodash imports to es-toolkit/compat for a smaller,
faster, lodash-compatible utility library. Transitive lodash usage from
other packages remains unchanged.

* fix: Restore isPlainObject semantics in CanCan policy

The lodash migration aliased `isObject` to `lodash/isPlainObject` and
the codemod incorrectly mapped the local name to es-toolkit's `isObject`,
which also returns true for arrays and functions. This caused condition
objects in policy definitions to be skipped, breaking authorization
checks across the codebase.

* fix: Restore unicode-aware length counting in validators

es-toolkit/compat's size() returns string.length, while lodash's _.size()
counts unicode code points. Switch to [...value].length to preserve the
previous behavior so multi-byte characters like emoji count as one.
2026-05-06 21:03:47 -04:00

165 lines
3.8 KiB
TypeScript

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 "styled-components-breakpoint";
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;
}
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 {
/**
* Click handler for button mode.
*/
onClick: () => void;
/**
* 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;
${breakpoint("tablet")`
padding: 6px 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 = {
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)"
);
const Tab: React.FC<Props> = (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)) {
return (
<TabButton $active={props.active} onClick={props.onClick}>
{children}
{props.active && (
<Active
layoutId="underline"
initial={false}
transition={transition}
transformTemplate={horizontalOnly}
/>
)}
</TabButton>
);
}
// Link mode - controlled by react-router
const { to, ...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>
);
};
export default Tab;