Skip to content

Commit

Permalink
Bring in CSS Hooks.
Browse files Browse the repository at this point in the history
  • Loading branch information
nsaunders committed Oct 17, 2023
1 parent 921b815 commit 5993844
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 35 deletions.
42 changes: 42 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"lint": "next lint"
},
"dependencies": {
"@css-hooks/react": "^1.0.2",
"@fontsource/lato": "^5.0.5",
"@fontsource/montserrat": "^5.0.5",
"@tsconfig/node20": "^1.0.2",
Expand Down
2 changes: 2 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Footer from "@/components/Footer";
import Surface from "@/components/Surface";
import exhausted from "@/utils/exhausted";
import { title, description } from "@/meta";
import { css } from "@/utils/css-hooks";

const lato = Lato({
weight: ["400", "700"],
Expand All @@ -28,6 +29,7 @@ export default function RootLayout({
{({ style, ...restProps }) =>
exhausted(restProps) && (
<body className={lato.className} style={{ ...style, margin: 0 }}>
<style>{css}</style>
<Providers>
<Header />
{children}
Expand Down
59 changes: 24 additions & 35 deletions src/components/Anchor.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
"use client";

import {
ComponentProps,
ComponentType,
CSSProperties,
FocusEvent,
MouseEvent,
ReactElement,
forwardRef,
} from "react";
import { O, U } from "ts-toolbelt";
import useHover from "@/hooks/useHover";
import useFocus from "@/hooks/useFocus";
import hooks from "@/utils/css-hooks";

export type ForwardProps = {
style?: CSSProperties;
tabIndex?: -1;
onClick?: (e: MouseEvent) => void;
onFocus?: (e: FocusEvent) => void;
onBlur?: () => void;
onMouseEnter?: () => void;
onMouseLeave?: () => void;
};

export type Props = U.Strict<
Expand All @@ -35,32 +25,31 @@ export type Props = U.Strict<

export default forwardRef<HTMLAnchorElement, O.Omit<Props, "ref">>(
function Anchor({ children, disabled, selected, style, ...restProps }, ref) {
const [focus, { onFocus, onBlur }] = useFocus();
const [hover, { onMouseEnter, onMouseLeave }] = useHover();

const forwardProps: ForwardProps = {
onClick(e) {
if (disabled) {
e.preventDefault();
}
},
onFocus,
onBlur,
onMouseEnter,
onMouseLeave,
style: {
textDecoration: hover && !focus ? "underline" : "none",
outlineColor: "transparent",
outlineWidth: 2,
outlineStyle: "dotted",
color: hover ? "var(--fg-link-bright)" : "var(--fg-link)",
boxShadow:
focus && !disabled
? "0 0 0 2px var(--bg), 0 0 0 4px var(--blue-500)"
: undefined,
borderRadius: 2,
...(selected && { color: "var(--blue-100)" }),
...(disabled && { cursor: "default", textDecoration: "none" }),
...hooks({
textDecoration: "none",
outlineColor: "transparent",
outlineWidth: 2,
outlineStyle: "dotted",
color: "var(--fg-link)",
borderRadius: 2,
...(disabled
? {
cursor: "default",
}
: {
hover: {
color: "var(--fg-link-bright)",
textDecoration: "underline",
},
focusVisible: {
boxShadow: "0 0 0 2px var(--bg), 0 0 0 4px var(--blue-500)",
textDecoration: "none",
},
}),
...(selected && { color: "var(--blue-100)" }),
}),
...style,
},
tabIndex: disabled ? -1 : undefined,
Expand Down
7 changes: 7 additions & 0 deletions src/utils/css-hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createHooks, recommended } from "@css-hooks/react";

const [css, hooks] = createHooks(recommended);

export default hooks;

export { css };

0 comments on commit 5993844

Please sign in to comment.