1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-09-16 16:15:33 -04:00

type-safe stitches util for fancy link underline color

This commit is contained in:
2022-07-31 11:24:44 -04:00
parent 43d4b2ea46
commit 1c53376988
3 changed files with 26 additions and 18 deletions

View File

@@ -1,6 +1,6 @@
import NextLink from "next/link";
import objStr from "obj-str";
import { styled, theme } from "../../lib/styles/stitches.config";
import { styled, theme, stitchesConfig } from "../../lib/styles/stitches.config";
import { baseUrl } from "../../lib/config";
import type { ComponentProps } from "react";
@@ -14,20 +14,20 @@ const StyledLink = styled(NextLink, {
true: {
// sets psuedo linear-gradient() for the underline's color; see stitches config for the weird calculation behind
// the local `$$underlineColor` variable.
setUnderlineVars: {},
...stitchesConfig.utils.setUnderlineColor({ color: "$colors$linkUnderline" }),
backgroundImage: `linear-gradient($$underlineColor, $$underlineColor)`,
backgroundImage: "linear-gradient($$underlineColor, $$underlineColor)",
backgroundPosition: "0% 100%",
backgroundRepeat: "no-repeat",
backgroundSize: `0% ${theme.borderWidths.underline}`,
paddingBottom: "0.2rem",
backgroundSize: "0% 2px",
paddingBottom: "3px",
"@media (prefers-reduced-motion: no-preference)": {
transition: `background-size ${theme.transitions.linkHover}`,
},
"&:hover, &:focus-visible": {
backgroundSize: `100% ${theme.borderWidths.underline}`,
backgroundSize: "100% 2px",
},
},
false: {},

View File

@@ -9,9 +9,18 @@ import normalizeStyles from "./utils/normalize";
import { Inter, RobotoMono } from "./fonts";
// https://stitches.dev/docs/typescript#type-a-css-object
export type CSS = Stitches.CSS<typeof config>;
export type CSS = Stitches.CSS<typeof stitchesConfig>;
export const { styled, css, getCssText, globalCss, keyframes, createTheme, theme, config } = createStitches({
export const {
styled,
css,
getCssText,
globalCss,
keyframes,
createTheme,
theme,
config: stitchesConfig,
} = createStitches({
theme: {
fonts: {
sans: `"${Inter.name.regular}", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif`,
@@ -56,11 +65,6 @@ export const { styled, css, getCssText, globalCss, keyframes, createTheme, theme
maxLayoutWidth: "865px",
},
borderWidths: {
// underline height is based on link's font size
underline: "calc(0.1em + 0.05rem)",
},
radii: {
corner: "0.6rem",
},
@@ -82,9 +86,13 @@ export const { styled, css, getCssText, globalCss, keyframes, createTheme, theme
},
utils: {
setUnderlineVars: ({
color = "$colors$linkUnderline", // see theme tokens above
// sets locally scoped css variable for both light and dark themes' link hover underlines
setUnderlineColor: ({
color,
alpha = 0.4,
}: {
color: string; // hex value or one of theme tokens above in stitches `$colors$token` format
alpha?: number;
}) => ({
// allow both pre-set rgba stitches variables and hex values
$$underlineColor: color.startsWith("#") ? hexToRgba(color, alpha) : color,

View File

@@ -1,5 +1,5 @@
import Link, { LinkProps } from "../components/Link";
import { styled, theme, darkTheme, keyframes } from "../lib/styles/stitches.config";
import { styled, theme, darkTheme, keyframes, stitchesConfig } from "../lib/styles/stitches.config";
const ColorfulLink = ({
lightColor,
@@ -14,11 +14,11 @@ const ColorfulLink = ({
<Link
css={{
color: lightColor,
setUnderlineVars: { color: lightColor },
...stitchesConfig.utils.setUnderlineColor({ color: lightColor }),
[`.${darkTheme} &`]: {
color: darkColor,
setUnderlineVars: { color: darkColor },
...stitchesConfig.utils.setUnderlineColor({ color: darkColor }),
},
...css,