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
59 changes: 53 additions & 6 deletions src/styles/timeline/timeline.css
Original file line number Diff line number Diff line change
Expand Up @@ -581,17 +581,20 @@
border-radius: 0 4px 4px 0;
}

.ss-clip-resize-handle:hover {
background: rgba(0, 0, 0, 0.08);
}

/* Visible resize handles when selected */
.ss-clip.selected .ss-clip-resize-handle {
background: rgba(0, 0, 0, 0.06);
}

.ss-clip.selected .ss-clip-resize-handle:hover {
background: rgba(0, 0, 0, 0.12);
/* Chrome pulls a nearby touch onto an element with hover styles, so only a hovering pointer gets them */
@media (hover: hover) {
.ss-clip-resize-handle:hover {
background-color: rgba(0, 0, 0, 0.08);
}

.ss-clip.selected .ss-clip-resize-handle:hover {
background-color: rgba(0, 0, 0, 0.12);
}
}

/*
Expand Down Expand Up @@ -663,6 +666,50 @@
}
}

/* Touch targets need a coarse primary pointer, so a mouse-first machine with a touchscreen keeps the desktop handles */
@media (pointer: coarse) {
.ss-clip {
/* A handle's hit area grows inward, to at most 35% of the clip so a short clip keeps a body to drag;
background-clip keeps its fill on the outer edge. */
--ss-touch-edge: min(clamp(24px, 30cqw, 44px), 35cqw);
}

.ss-clip.selected .ss-clip-resize-handle {
box-sizing: content-box;
background-clip: content-box;
}

.ss-clip.selected .ss-clip-resize-handle.left {
padding-right: calc(var(--ss-touch-edge) - 12px);
}

.ss-clip.selected .ss-clip-resize-handle.right {
padding-left: calc(var(--ss-touch-edge) - 12px);
}

.ss-clip.selected .ss-clip-menu-trigger::before {
content: "";
position: absolute;
inset: -4px 0 -4px -8px;
}

.ss-clip:not(.selected) .ss-clip-resize-handle {
display: none;
}

.ss-clip:not(.selected) .ss-clip-menu-trigger {
display: none;
}

.ss-clip-menu-trigger {
right: var(--ss-touch-edge);
}

.ss-clip.selected .ss-clip-content {
padding-right: calc(var(--ss-touch-edge) + 24px);
}
}

/* Playhead */
.ss-playhead {
position: absolute;
Expand Down
146 changes: 146 additions & 0 deletions tests/touch-hit-areas.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/**
* Touch hit areas. jsdom has no layout and ignores media queries, so this guards the rules the design rests on:
* everything here needs a coarse primary pointer, and the hit areas themselves only reshape boxes.
* What a finger can actually reach is measured in a real browser.
*/

// @ts-expect-error - Jest transforms this via moduleNameMapper to css-inline.js mock
import styles from "../src/styles/index.css?inline";

/** The text between the braces of every at-rule starting with `query`, found by matching braces. */
function mediaBlocks(css: string, query: string): string[] {
const blocks: string[] = [];
let from = css.indexOf(query);
while (from !== -1) {
const open = css.indexOf("{", from);
let depth = 1;
let i = open + 1;
while (depth > 0 && i < css.length) {
if (css[i] === "{") depth += 1;
if (css[i] === "}") depth -= 1;
i += 1;
}
blocks.push(css.slice(open + 1, i - 1));
from = css.indexOf(query, i);
}
return blocks;
}

interface Rule {
selector: string;
declarations: Record<string, string>;
}

function rulesIn(blocks: string[]): Rule[] {
return blocks.flatMap(block =>
[...block.replace(/\/\*[\s\S]*?\*\//g, "").matchAll(/([^{}]+)\{([^{}]*)\}/g)].flatMap(([, selectors, body]) =>
selectors.split(",").map(selector => ({
selector: selector.trim().replace(/\s+/g, " "),
declarations: Object.fromEntries(
body
.split(";")
.map(declaration => declaration.split(/:(.*)/s).map(part => part.trim()))
.filter(([property, value]) => property && value)
)
}))
)
);
}

const primaryCoarse = rulesIn(mediaBlocks(styles, "@media (pointer: coarse)"));
const declared = (rules: Rule[], selector: string): Record<string, string> =>
Object.assign({}, ...rules.filter(rule => rule.selector === selector).map(rule => rule.declarations));

// The hit areas: the touch edge, the selected clip's padded handles and the trigger's reach. Hiding handles and moving
// the trigger are visible changes, so they are not held to this.
const CLIP_RULES = primaryCoarse.filter(
rule => rule.selector === ".ss-clip" || /^\.ss-clip\.selected \.ss-clip-(resize-handle|menu-trigger::before)/.test(rule.selector)
);
// Each property is allowed only on the selectors where it reshapes a box without painting: a handle's padding, and an
// empty pseudo-element. Anywhere else it could move the trigger or pad the label.
const RESHAPES_ONLY_ON: Record<string, RegExp> = {
"box-sizing": /resize-handle/,
"background-clip": /resize-handle/,
"padding-left": /resize-handle/,
"padding-right": /resize-handle/,
content: /::before$/,
position: /::before$/,
inset: /::before$/
};
const unselectedHandle = ".ss-clip:not(.selected) .ss-clip-resize-handle";
const unselectedTrigger = ".ss-clip:not(.selected) .ss-clip-menu-trigger";
const triggerReach = ".ss-clip.selected .ss-clip-menu-trigger::before";

describe("touch hit areas", () => {
it("finds the rules it guards", () => {
expect(CLIP_RULES.length).toBeGreaterThan(0);
expect(primaryCoarse.length).toBeGreaterThan(0);
});

it("only reshapes boxes for the hit areas, painting nothing", () => {
const disallowed = CLIP_RULES.flatMap(rule =>
Object.keys(rule.declarations)
.filter(property => !property.startsWith("--") && !RESHAPES_ONLY_ON[property]?.test(rule.selector))
.map(property => `${rule.selector} { ${property} }`)
);

expect(disallowed).toEqual([]);
expect(CLIP_RULES.flatMap(rule => (rule.declarations["content"] ? [rule.declarations["content"]] : []))).toEqual(['""']);
});

it("leaves every mouse-first machine alone, touchscreen or not", () => {
// any-pointer also matches a touchscreen laptop driven by a mouse, in whatever media condition it appears
const anyPointerQueries = [...new Set<string>(styles.match(/@media[^{]*any-pointer[^{]*/g) ?? [])];
expect(
rulesIn(anyPointerQueries.flatMap(query => mediaBlocks(styles, query)))
.map(rule => rule.selector)
.filter(selector => selector.startsWith(".ss-clip"))
).toEqual([]);
[".ss-clip.selected .ss-clip-resize-handle.left", ".ss-clip.selected .ss-clip-resize-handle.right", triggerReach].forEach(selector =>
expect(primaryCoarse.map(rule => rule.selector)).toContain(selector)
);
});

it("keeps each handle's fill on its outer edge, with no background shorthand on hover and no !important", () => {
expect(declared(CLIP_RULES, ".ss-clip.selected .ss-clip-resize-handle")["background-clip"]).toBe("content-box");
// The `background` shorthand would reset background-clip on hover
const hoverHandles = rulesIn(mediaBlocks(styles, "@media (hover: hover)")).filter(rule => rule.selector.includes("ss-clip-resize-handle"));
expect(hoverHandles.length).toBeGreaterThan(0);
expect(hoverHandles.filter(rule => "background" in rule.declarations)).toEqual([]);
expect(CLIP_RULES.flatMap(rule => Object.values(rule.declarations)).filter(value => value.includes("!important"))).toEqual([]);
});

it("styles a trim handle's hover only where the primary pointer hovers", () => {
// Chrome's touch adjustment pulls a nearby touch onto an element with hover styles. Rules are compared as a list,
// not by name, because the same selector outside the hover block is exactly the rule this guards against.
const handleHover = (rules: Rule[]) =>
rules.map(rule => rule.selector).filter(selector => /ss-clip-resize-handle.*:(hover|active)/.test(selector));
const scoped = handleHover(rulesIn(mediaBlocks(styles, "@media (hover: hover)")));

expect(scoped.length).toBeGreaterThan(0);
expect(handleHover(rulesIn([styles]))).toEqual(scoped);
});

it("gives each handle at least 24px, under half the clip", () => {
const edge = declared(CLIP_RULES, ".ss-clip")["--ss-touch-edge"];

expect(Number(edge.match(/clamp\((\d+)px/)?.[1])).toBeGreaterThanOrEqual(24);
expect(Number(edge.match(/(\d+)cqw\)$/)?.[1])).toBeLessThan(50);
});

it("reaches the trigger's hit area to 24px, away from the handle", () => {
const [top, right, bottom, left] = declared(CLIP_RULES, triggerReach)["inset"].split(" ").map(parseFloat);

expect(16 - left - right).toBeGreaterThanOrEqual(24);
expect(16 - top - bottom).toBeGreaterThanOrEqual(24);
expect(right).toBe(0);
});

it("hides handles and the trigger on unselected clips only where the primary pointer is coarse", () => {
[unselectedHandle, unselectedTrigger].forEach(selector => expect(declared(primaryCoarse, selector)["display"]).toBe("none"));
});

it("moves the menu trigger out of the handle's reach where the primary pointer is coarse", () => {
expect(declared(primaryCoarse, ".ss-clip-menu-trigger")["right"]).toBe("var(--ss-touch-edge)");
});
});
Loading