Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ The root layout loads Simple Analytics for page views. A delegated click listene
- `/important` is a seven-round specificity boss battle with hints, explanations, actual isolated CSS previews, scoring and replay. A quiet `{ !important }` footer link and an exact `!important` search lead to it. The examples cover matching, unlayered author declarations; the page links to MDN for the wider cascade and stays out of the sitemap.
- Search `c0ffee` or `#c0ffee` for a short, replayable coffee party. The non-modal message can be dismissed, and reduced-motion visitors get a still celebration. Ordinary search results remain available.
- The favicon sleeps when the tab is hidden and restores the original icons on return, including icons replaced during navigation. It uses a local SVG.
- Drag the fxcss logo in the header or footer out past its edge and it becomes `fxcss pick` for the site. Until the pointer leaves the logo the press stays a click, however much it wobbles, so a shaky click still goes home. Once lifted, whatever it is over gets the picker's pink outline, with the element's classes labelled underneath (above, near the bottom of the window). It labels the nearest element that has a class. When some of an element's classes come from the site's own stylesheet rather than Tailwind's utility layer, only those are shown; utility-only lists are shortened. Release it over an element to pick it, as a click does in `fxcss pick`: a terminal-style card beside the element prints the CLI's report for it, with its tag and selector, classes (the site's own first), box, notable computed styles and the rules on the site that match it, with rules naming one of its classes listed first and Tailwind's layered rules marked by layer. The card copies the selector and links to the `fxcss pick` documentation. It looks like the terminal of the current time-machine stop: an MS-DOS Prompt in 1999, Terminal on Mac OS X in 2003, and an xterm in 2002. The report itself follows the page, so it lists the time machine's `html[data-retro]` rules when they apply. It is a non-modal dialog that takes focus; Escape or its close button returns focus to the logo, and a press elsewhere or leaving the page closes it. Carry the logo back onto itself, release it outside the page, or press Escape during the drag, and nothing is picked or followed. Either way the logo returns; reduced-motion visitors skip the flight and the card's fade. Dropping it activates nothing underneath, and a click without a drag still goes home. Mouse and pen only, so touch scrolling and taps behave as before.
- Drag the fxcss logo in the header or footer out past its edge and it becomes `fxcss pick` for the site. Until the pointer leaves the logo the press stays a click, however much it wobbles, so a shaky click still goes home. Once lifted, whatever it is over gets the picker's pink outline, with the element's classes labelled underneath (above, near the bottom of the window). It labels the nearest element that has a class. When some of an element's classes come from the site's own stylesheet rather than Tailwind's utility layer, only those are shown; utility-only lists are shortened. Release it over an element to pick it, as a click does in `fxcss pick`: a terminal-style card beside the element prints the CLI's report for it, with its tag and selector, classes (the site's own first), box, notable computed styles and the rules on the site that match it, with rules naming one of its classes listed first and Tailwind's layered rules marked by layer. Each part of the report has its own colour (labels, property names, values, classes, attribute tests and pseudo-classes), colour values get a swatch, and style values line up in a column; every colour meets 4.5:1 in each terminal. The card copies the selector and links to the `fxcss pick` documentation. It looks like the terminal of the current time-machine stop: an MS-DOS Prompt in 1999, Terminal on Mac OS X in 2003, and an xterm in 2002. The report itself follows the page, so it lists the time machine's `html[data-retro]` rules when they apply. It is a non-modal dialog that takes focus; Escape or its close button returns focus to the logo, and a press elsewhere or leaving the page closes it. Carry the logo back onto itself, release it outside the page, or press Escape during the drag, and nothing is picked or followed. Either way the logo returns; reduced-motion visitors skip the flight and the card's fade. Dropping it activates nothing underneath, and a click without a drag still goes home. Mouse and pen only, so touch scrolling and taps behave as before.

## Screenshot evidence

Expand Down
67 changes: 61 additions & 6 deletions app/components/pick-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import {
import { createPortal } from 'react-dom';
import { Check, Copy, X } from 'lucide-react';
import { labelPosition } from '../../lib/logo-picker';
import { reportLines, type PickReport } from '../../lib/pick-report';
import {
reportTokens,
type PickReport,
type Token,
} from '../../lib/pick-report';

/**
* What a dropped logo picked, printed the way `fxcss pick` prints a click.
Expand All @@ -34,7 +38,7 @@ export function PickCard({
const [status, setStatus] = useState('');
const statusTimer = useRef<ReturnType<typeof setTimeout>>(undefined);
const close = useEffectEvent(onClose);
const [header, ...details] = reportLines(report);
const [header, ...details] = reportTokens(report);

// Beside the element, as the label was, and still beside it after scrolling.
useLayoutEffect(() => {
Expand Down Expand Up @@ -127,10 +131,24 @@ export function PickCard({
</div>
<pre className="pick-card-report">
{/* One block per line, so a wrapped rule hangs under its own start. */}
<strong>{header}</strong>
{details.map((line, index) => (
<span key={index}>{line}</span>
))}
<strong>
<Tokens line={header} />
</strong>
{details.map((line, index) => {
const hang = valueColumn(line);
return (
<span
key={index}
style={
hang === undefined
? undefined
: { paddingLeft: `${hang}ch`, textIndent: `-${hang}ch` }
}
>
<Tokens line={line} />
</span>
);
})}
</pre>
<div className="pick-card-actions">
<button type="button" onClick={copy}>
Expand All @@ -150,3 +168,40 @@ export function PickCard({
element.ownerDocument.body,
);
}

/**
* Where a labelled line's values start, so a long value that wraps stays in
* its column. Other lines, such as rules, hang by the stylesheet's default.
*/
function valueColumn(line: Token[]) {
const lead = line.find(({ kind }) => kind !== 'text');
if (lead?.kind !== 'label' && lead?.kind !== 'property') return undefined;
const start = line.findIndex(({ kind }) =>
['value', 'colour', 'class'].includes(kind),
);
if (start < 0) return undefined;
return line
.slice(0, start)
.reduce((width, { text }) => width + text.length, 0);
}

/** One line of the report, coloured by what each piece is. */
function Tokens({ line }: { line: Token[] }) {
return line.map(({ kind, text }, index) =>
kind === 'text' ? (
text
) : (
<span key={index} className={`pick-token-${kind}`}>
{kind === 'colour' && (
// Painted over a checkerboard, so a translucent colour shows as one.
<span
className="pick-swatch"
style={{ boxShadow: `inset 0 0 0 1em ${text}` }}
aria-hidden="true"
/>
)}
{text}
</span>
),
);
}
14 changes: 14 additions & 0 deletions app/easter-eggs.css
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ html[data-logo-picking], html[data-logo-picking] * { cursor: grabbing !important
.pick-card-report { min-height: 0; margin: 0; padding: 10px 12px; overflow: auto; font: inherit; white-space: pre-wrap; overflow-wrap: anywhere; }
.pick-card-report strong { color: #ff6fb5; font-weight: 700; }
.pick-card-report > * { display: block; padding-left: 4ch; text-indent: -4ch; }
/* Each kind of token keeps one colour, so a property, its value and the
classes in a selector read apart. Every colour meets 4.5:1 on its console. */
.pick-card { --pick-label: #8d9abd; --pick-property: #7cc7ff; --pick-value: #ffd27a; --pick-class: #86e1b0; --pick-attribute: #c9a6ff; }
.pick-token-label, .pick-token-muted { color: var(--pick-label); }
.pick-token-muted { font-style: italic; }
.pick-token-property { color: var(--pick-property); }
.pick-token-value, .pick-token-colour { color: var(--pick-value); }
.pick-token-class { color: var(--pick-class); }
.pick-token-attribute, .pick-token-pseudo { color: var(--pick-attribute); }
.pick-card-report strong .pick-token-muted { font-style: normal; }
.pick-swatch { display: inline-block; width: 0.9em; height: 0.9em; margin-right: 0.4em; border: 1px solid var(--pick-label); border-radius: 2px; vertical-align: -0.1em; background: repeating-conic-gradient(#bbb 0 25%, #fff 0 50%) 0 0 / 6px 6px; text-indent: 0; }
.pick-card-prompt::before { content: '$ '; }
.pick-card-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 14px; padding: 8px 12px 10px; border-top: 1px solid #2b3350; }
.pick-card-actions button { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border: 1px solid #ff4fa7; border-radius: 4px; background: #db0078; color: #fff; font: inherit; cursor: pointer; }
Expand All @@ -169,6 +180,7 @@ html[data-retro='windows'] .pick-card-close { padding: 0 1px; border: 2px outset
html[data-retro='windows'] .pick-card-close:active { border-style: inset; }
html[data-retro='windows'] .pick-card-report { margin: 2px 0; border: 2px inset #eee; background: #000; color: #c0c0c0; }
html[data-retro='windows'] .pick-card-report strong { color: #ff55ff; }
html[data-retro='windows'] .pick-card { --pick-label: #a8a8a8; --pick-property: #55ffff; --pick-value: #ffff55; --pick-class: #55ff55; --pick-attribute: #ff5555; }
html[data-retro='windows'] .pick-card-actions { border: 0; padding: 6px 4px 4px; font-family: Tahoma, Verdana, sans-serif; }
html[data-retro='windows'] .pick-card-actions button { border: 2px outset #eee; border-radius: 0; background: #c0c0c0; color: #111; }
html[data-retro='windows'] .pick-card-actions button:active { border-style: inset; }
Expand All @@ -185,6 +197,7 @@ html[data-retro='mac'] .pick-card-close svg { width: 9px; height: 9px; opacity:
html[data-retro='mac'] .pick-card-close:is(:hover, :focus-visible) svg { opacity: 1; }
html[data-retro='mac'] .pick-card-report { background: #fff; color: #111; }
html[data-retro='mac'] .pick-card-report strong { color: #b8005f; }
html[data-retro='mac'] .pick-card { --pick-label: #6b6b6b; --pick-property: #1d55b0; --pick-value: #8a4600; --pick-class: #1c6b36; --pick-attribute: #7a2bb5; }
html[data-retro='mac'] .pick-card-actions { border-top-color: #c6c6c6; font-family: 'Lucida Grande', 'Lucida Sans Unicode', Arial, sans-serif; }
html[data-retro='mac'] .pick-card-actions button { padding: 3px 14px; border: 1px solid #3a6fb0; border-radius: 999px; background: linear-gradient(#d8ecff, #8cc2f8 50%, #6aaef5 50%, #b6dcff); color: #111; }
html[data-retro='mac'] .pick-card-actions button:hover { filter: brightness(1.05); }
Expand All @@ -198,6 +211,7 @@ html[data-retro='linux'] .pick-card-close { border: 1px solid #2c3a4c; border-ra
html[data-retro='linux'] .pick-card-close:hover { background: #fff; color: #111; }
html[data-retro='linux'] .pick-card-report { background: #000; color: #e5e5e5; }
html[data-retro='linux'] .pick-card-report strong { color: #ff55ff; }
html[data-retro='linux'] .pick-card { --pick-label: #a8a8a8; --pick-property: #00cdcd; --pick-value: #cdcd00; --pick-class: #00cd00; --pick-attribute: #ff6464; }
html[data-retro='linux'] .pick-card-actions { border-top-color: #a9a69b; font-family: 'DejaVu Sans', 'Liberation Sans', Arial, sans-serif; }
html[data-retro='linux'] .pick-card-actions button { border: 1px solid #8a877d; border-radius: 3px; background: linear-gradient(#f4f3ef, #dedcd4); color: #111; }
html[data-retro='linux'] .pick-card-actions button:hover { background: #fff; }
Expand Down
151 changes: 122 additions & 29 deletions lib/pick-report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,41 +173,134 @@ export function pickReport(element: Element): PickReport {
};
}

/** The report as `fxcss pick` would print it in a terminal. */
export function reportLines(report: PickReport) {
const { box, rules } = report;
const lines = [`${report.tag} → ${report.label}`];
const { classes } = report;
if (classes.length)
lines.push(
`classes ${classes.slice(0, SHOWN_CLASSES).join(' ')}` +
(classes.length > SHOWN_CLASSES
? ` +${classes.length - SHOWN_CLASSES} more`
: ''),
);
lines.push(
`box ${box.width}×${box.height} at (${box.x}, ${box.y})` +
(box.width && box.height ? '' : ' (not visible in this layout)'),
/** What a piece of the report is, so the card can colour it. */
export type TokenKind =
| 'text'
| 'label'
| 'muted'
| 'tag'
| 'heading'
| 'class'
| 'attribute'
| 'pseudo'
| 'property'
| 'value'
| 'colour';
export type Token = { kind: TokenKind; text: string };

const token = (kind: TokenKind, text: string): Token => ({ kind, text });

/** Pieces of text matched by a pattern, with the text between them. */
function split(
text: string,
pattern: RegExp,
kindOf: (match: RegExpMatchArray) => TokenKind,
between: TokenKind,
) {
const tokens: Token[] = [];
let last = 0;
for (const match of text.matchAll(pattern)) {
if (match.index > last)
tokens.push(token(between, text.slice(last, match.index)));
tokens.push(token(kindOf(match), match[0]));
last = match.index + match[0].length;
}
if (last < text.length) tokens.push(token(between, text.slice(last)));
return tokens;
}

/** A selector with its classes, attribute tests and pseudo-classes marked. */
export function selectorTokens(selector: string) {
return split(
selector,
/(\.(?:\\.|[\w-])+)|(\[[^\]]*\])|(::?[\w-]+)/g,
(match) => (match[1] ? 'class' : match[2] ? 'attribute' : 'pseudo'),
'text',
);
}

/** A computed value with each colour in it marked, for a swatch. */
export function valueTokens(value: string) {
return split(
value,
/\b(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\([^)]*\)/g,
() => 'colour',
'value',
);
if (report.styles.length) {
lines.push('styles');
for (const [property, value] of report.styles)
lines.push(` ${property}: ${value}`);
}

/**
* The report as `fxcss pick` prints it, one array of tokens per line. Style
* values line up in a column, which the terminal leaves ragged.
*/
export function reportTokens(report: PickReport): Token[][] {
const { box, classes, rules, styles } = report;
const lines: Token[][] = [
[
token('tag', report.tag),
token('muted', ' → '),
token('heading', report.label),
],
];
if (classes.length)
lines.push([
token('label', 'classes '),
...classes
.slice(0, SHOWN_CLASSES)
.flatMap((name, index) => [
...(index ? [token('text', ' ')] : []),
token('class', name),
]),
...(classes.length > SHOWN_CLASSES
? [token('muted', ` +${classes.length - SHOWN_CLASSES} more`)]
: []),
]);
lines.push([
token('label', 'box '),
token('value', `${box.width}×${box.height}`),
token('label', ' at '),
token('value', `(${box.x}, ${box.y})`),
...(box.width && box.height
? []
: [token('muted', ' (not visible in this layout)')]),
]);
if (styles.length) {
lines.push([token('label', 'styles')]);
const width = Math.max(...styles.map(([property]) => property.length));
for (const [property, value] of styles)
lines.push([
token('text', ' '),
token('property', property),
token('muted', ':'.padEnd(width - property.length + 2)),
...valueTokens(value),
]);
}
if (!rules.length) {
lines.push('styled by no rules on this site (browser defaults only)');
lines.push([
token('label', 'styled by no rules on this site (browser defaults only)'),
]);
return lines;
}
lines.push(
`styled by ${rules.length} rule${rules.length === 1 ? '' : 's'} on this site`,
);
lines.push([
token(
'label',
`styled by ${rules.length} rule${rules.length === 1 ? '' : 's'} on this site`,
),
]);
for (const rule of rules.slice(0, SHOWN_RULES))
lines.push(
rule.layer
? ` @layer ${rule.layer} ${rule.selector}`
: ` ${rule.selector}`,
);
lines.push([
token('text', ' '),
...(rule.layer ? [token('muted', `@layer ${rule.layer} `)] : []),
...selectorTokens(rule.selector),
]);
if (rules.length > SHOWN_RULES)
lines.push(` … and ${rules.length - SHOWN_RULES} more`);
lines.push([token('muted', ` … and ${rules.length - SHOWN_RULES} more`)]);
return lines;
}

/** The report as plain text, one string per line. */
export function reportLines(report: PickReport) {
return reportTokens(report).map((line) =>
line.map(({ text }) => text).join(''),
);
}
29 changes: 28 additions & 1 deletion tests/browser/logo-picker.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,34 @@ test('dropping the logo prints the fxcss pick report and copies the selector', a
),
),
).toHaveCount(1);
await expect(line(/^ {2}color: rgb/)).toHaveCount(1);
const colour = line(/^ {2}color:\s+rgb/);
await expect(colour).toHaveCount(1);
// Properties, values and classes are coloured apart; colours get a swatch.
const tint = (part: string) =>
report
.locator(part)
.first()
.evaluate((element) => getComputedStyle(element).color);
const tints = new Set([
await tint('.pick-token-property'),
await tint('.pick-token-value, .pick-token-colour'),
await tint('.pick-token-class'),
await tint('.pick-token-label'),
]);
expect(tints.size, 'four distinct colours').toBe(4);
await expect(colour.locator('.pick-swatch')).toHaveAttribute(
'aria-hidden',
'true',
);
expect(
await colour
.locator('.pick-swatch')
.evaluate((element) => getComputedStyle(element).boxShadow),
).toMatch(/^rgb.* inset$/);
// A wrapped value stays under its own column.
expect(
await colour.evaluate((element) => getComputedStyle(element).textIndent),
).toMatch(/^-\d/);
const styledBy = line(/^styled by \d+ rules? on this site$/);
await expect(styledBy).toHaveCount(1);
// Rules naming the element's class come first.
Expand Down
Loading
Loading