176 web animation techniques, each a live demo with plain settings and a ready-made prompt for your AI assistant. No dependencies, no build step.
HTML ai-promptsanimationcss-animationsfrontendgithub-pagesjavascriptmicrointeractionsmotion-designno-dependenciesreferencescroll-animationui-animationweb-animationweb-developmentwebgl
feat: opened from disk, a page's links to folders (home, Previous, Next) open each folder's index.html instead of its listing
matin3b981f7254a9
diff --git a/assets/js/demo-page.js b/assets/js/demo-page.jsindex 4a2dcddd..b4734a20 100644--- a/assets/js/demo-page.js+++ b/assets/js/demo-page.js@@ -6,6 +6,7 @@* takes over ("hb:input"), and follows reduced motion (Loop and Slow motion greyed out,* loops start paused).* On scroll pages Play scrolls the box, Back to top jumps to its top and the visitor's own input stops it.+ * Opened from disk (file:), the page's links to folders open each folder's index.html instead of its listing.* The pure helpers are exported for tests/demo-page.test.js. */(function (root, factory) {var api = factory();@@ -39,6 +40,15 @@!/^[a-z][a-z0-9+.-]*:/i.test(href.replace(/^https?:/i, ''));}+ // Opened from disk, a link to a folder shows the folder's listing, not its page. This is the link as it must be written+ // there: a relative link whose path ends with "/" ("../bounce-in/", "../../../") names that folder's index.html and keeps+ // its ?query and #hash. Any other link (a file, "#top", "/…", "//…" or one with a scheme such as https:) comes back as it is.+ function indexHref(href) {+ var s = String(href), cut = s.search(/[?#]/), path = cut < 0 ? s : s.slice(0, cut);+ if (!/\/$/.test(path) || /^(?:\/|[a-z][a-z0-9+.-]*:)/i.test(path)) return s;+ return path + 'index.html' + s.slice(path.length);+ }+// Inline Markdown from README prose. Inline code becomes plain text: the site shows no code.function inline(md) {return escapeHtml(md)@@ -310,6 +320,16 @@});}+ // Opened from disk, every link of the page to a folder (the home link, Previous and Next, any other) opens that folder's+ // index.html instead of its listing. Similar animations is not shown there (the README is not read from disk), so every+ // link is in place by now.+ function openFoldersAsPages() {+ Array.prototype.forEach.call(doc.querySelectorAll('a[href]'), function (a) {+ var href = a.getAttribute('href'), fixed = indexHref(href);+ if (fixed !== href) a.setAttribute('href', fixed);+ });+ }+// What it is and Similar animations from the README. Opened from disk, the README link stays.function loadReadme() {var what = page.querySelector('.hb-what-text');@@ -475,6 +495,7 @@}}+ if (win.location.protocol === 'file:') openFoldersAsPages();setUpPrompt();if (copyBtn) setUpCopy();if (tryStep) ['input', 'change', 'click'].forEach(function (type) { tryStep.addEventListener(type, onSettingsChange); });@@ -511,7 +532,7 @@}return {- escapeHtml: escapeHtml, plain: plain, isSafeHref: isSafeHref, inline: inline, sections: sections,+ escapeHtml: escapeHtml, plain: plain, isSafeHref: isSafeHref, indexHref: indexHref, inline: inline, sections: sections,paragraphs: paragraphs, seeAlso: seeAlso, settingsLine: settingsLine, promptWithSettings: promptWithSettings,pageCopyText: pageCopyText, markFill: markFill, shorten: shorten,quoteText: quoteText, motionNote: motionNote, readSettings: readSettings, boot: bootdiff --git a/tests/demo-page.test.js b/tests/demo-page.test.jsindex 40bf8814..bd488f96 100644--- a/tests/demo-page.test.js+++ b/tests/demo-page.test.js@@ -21,6 +21,25 @@ test('isSafeHref allows relative, anchor and http(s) links only', () => {}});+test('indexHref points a relative link to a folder at its index.html, keeping its query and hash, and leaves other links alone', () => {+ const folders = {+ '../bounce-in/': '../bounce-in/index.html',+ '../../../': '../../../index.html',+ './': './index.html',+ 'animations/02-entrance-and-exit/bounce-in/': 'animations/02-entrance-and-exit/bounce-in/index.html',+ '../bounce-in/?from=home': '../bounce-in/index.html?from=home',+ '../bounce-in/#hb-prompt-title': '../bounce-in/index.html#hb-prompt-title',+ '../../../?place=buttons#top': '../../../index.html?place=buttons#top'+ };+ for (const [href, page] of Object.entries(folders)) assert.equal(DP.indexHref(href), page, href);+ // Files, fragments, queries and links to other places stay as they are; so does a link that already names its page.+ for (const href of ['README.md', '../bounce-in/index.html', '#top', '#', '', '?q=fade', '../bounce-in#a/', '../bounce-in?next=/',+ 'https://github.com/matinmonshizadeh/animation-handbook', 'https://example.com/a/', 'http://example.com/', '//example.com/a/',+ '/animation-handbook/', 'mailto:[email protected]', 'file:///C:/animation-handbook/']) {+ assert.equal(DP.indexHref(href), href, href);+ }+});+test('inline shows code as plain text and renders links, bold and italics', () => {assert.equal(DP.inline('Use `opacity` on **one** *hero* [card](../scale-in/)'),'Use opacity on <strong>one</strong> <em>hero</em> <a href="../scale-in/">card</a>');@@ -107,8 +126,9 @@ test('motionNote says what reduced motion changes in the player bar', () => {// a stage and, by kind, the buttons boot() looks for. Events are sent to the stage by hand (with isTrusted set as a browser// would), timers wait in a list until the test runs them, and every hb:input the script sends is kept. A Copy prompt test passes// `copy` ({ prompt, controls, clipboard }) to add a Copy prompt button, text in the prompt, a Try it step holding `controls` (if-// any) and the window's clipboard; no other test gets any of them.-function standInPage(kind, reduced, bodyAttributes, copy) {+// any) and the window's clipboard; no other test gets any of them. A link test passes `links` ({ protocol, hrefs }): the page is+// served over that protocol (http: otherwise) and holds one link for each href.+function standInPage(kind, reduced, bodyAttributes, copy, links) {const timers = [], sent = [];const node = () => ({attrs: {}, handlers: {}, children: [], clicks: 0, textContent: '', innerHTML: '', classList: { add() {}, remove() {}, toggle() {} },@@ -133,16 +153,18 @@ function standInPage(kind, reduced, bodyAttributes, copy) {body.setAttribute('data-hb-kind', kind);body.setAttribute('data-hb-autoplay', '');Object.keys(bodyAttributes || {}).forEach(name => body.setAttribute(name, bodyAttributes[name]));- const doc = { body, querySelector: selector => (selector === '.hb-page' ? page : null), createElement: node, dispatchEvent: event => sent.push(event) };+ const anchors = ((links && links.hrefs) || []).map(href => { const a = node(); a.setAttribute('href', href); return a; });+ const doc = { body, querySelector: selector => (selector === '.hb-page' ? page : null), createElement: node, dispatchEvent: event => sent.push(event),+ querySelectorAll: selector => (selector === 'a[href]' ? anchors : []) };const win = {matchMedia: () => ({ matches: !!reduced, addEventListener() {} }),setTimeout: (fn, ms) => timers.push({ fn, ms }),- clearTimeout() {}, location: { protocol: 'http:' }, console,+ clearTimeout() {}, location: { protocol: (links && links.protocol) || 'http:' }, console,CustomEvent: class { constructor(type, init) { this.type = type; this.detail = init && init.detail; } }};if (copy) Object.assign(win, { navigator: { clipboard: copy.clipboard }, getComputedStyle: () => ({ display: '' }) });DP.boot(doc, win);- return { doc, win, stage, player, demo, button, sent, runTimers: () => timers.forEach(t => t.fn()), timerDelays: () => timers.map(t => t.ms) };+ return { doc, win, stage, player, demo, button, anchors, sent, runTimers: () => timers.forEach(t => t.fn()), timerDelays: () => timers.map(t => t.ms) };}const visitor = { isTrusted: true }, script = { isTrusted: false };@@ -233,6 +255,22 @@ test('a scroll page with an empty sentence of its own gets the usual note, and oassert.equal(standInPage('scroll', false, { 'data-hb-motion-note': 'The layers stay still while the box scrolls' }).player.children.length, 0);});+// Opened from disk, Chrome shows a folder's listing for a link to the folder, so the links of the page must name index.html there.+test('opened from disk, every link of the page to a folder opens its index.html; over http(s) no link changes', () => {+ const hrefs = ['../../../', '../bounce-in/', '../scale-in/', '../blur-in/?from=home#hb-prompt-title', 'README.md', '#top',+ 'https://github.com/matinmonshizadeh/animation-handbook'];+ const fromDisk = ['../../../index.html', '../bounce-in/index.html', '../scale-in/index.html', '../blur-in/index.html?from=home#hb-prompt-title',+ 'README.md', '#top', 'https://github.com/matinmonshizadeh/animation-handbook'];+ for (const kind of ['once', 'loop', 'do', 'scroll']) {+ const page = standInPage(kind, false, {}, null, { protocol: 'file:', hrefs });+ assert.deepEqual(page.anchors.map(a => a.getAttribute('href')), fromDisk, `${kind} page opened from disk`);+ }+ for (const protocol of ['http:', 'https:']) {+ const page = standInPage('once', false, {}, null, { protocol, hrefs });+ assert.deepEqual(page.anchors.map(a => a.getAttribute('href')), hrefs, protocol);+ }+});+test('promptWithSettings adds the settings line only when a setting is complete', () => {assert.equal(DP.promptWithSettings('Add it.', [{ label: 'Speed', value: 'Normal' }, { label: 'Size', value: 'Big' }]),'Add it.\n\nSettings from the demo: Speed: Normal, Size: Big.');