components
All repositories: gitoria
10.4 KB
// date-picker.hl — THE DATE PICKER for input fields, all Hybriel (no JavaScript file): `DatePicker { names = "arrival,departure" }`// makes one text field per name; click a field, a month opens under it, click a day and the field shows it as YYYY-MM-DD.// Typing works too. Each field posts in a form by its name. The month grid is built here (plain integers, so the server render// and the browser agree); the browser's own today and language come in when a field gets focus (`Date` and `Intl` are browser// globals of View handlers). The popup is shown by CSS while its field or the popup has focus (:focus-within), so no `if`// re-creates the DOM. Use ONE `DatePicker` per page (a composed component's members are the page's state) — give it all the fields.import { now, timestamp } from 'hl:time'// the server's today (UTC) for the first render only; the browser corrects it when a field gets focusserverToday = () => { return timestamp(now()).slice(0, 10) }String names = 'date'Hybrid fields = names.split(',')Hybrid values = blanks(fields)Hybrid rows = makeRows(fields)String today = serverToday()Number year = toNumber(today.slice(0, 4))Number month = toNumber(today.slice(5, 7))Hybrid monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']Hybrid weekdayNames = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']Hybrid weekdays = weekdayNamesString title = monthNames[month - 1] + ' ' + yearString selected = ''Hybrid days = build(year, month, today, '')String prevLabel = 'Previous month'String nextLabel = 'Next month'String placeholder = 'YYYY-MM-DD'blanks(list) {out = []let i = 0while (i < list.length) { out.push('') i = i + 1 }return out}// `rows` is never reassigned: re-rendering the rows would re-create the input elements, and a re-created input on a slow// round trip shows an empty value although its attribute is set. A field's text and its invalid mark are written to the// element itself (see field()) instead of being bound.makeRows(list) {out = []let i = 0while (i < list.length) {out.push({ name = list[i] hint = list[i] + ', ' + placeholder })i = i + 1}return out}pad(n) { return n < 10 ? '0' + n : '' + n }leap(y) { return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 }daysIn(y, m) {if (m == 2) { return leap(y) ? 29 : 28 }if (m == 4 || m == 6 || m == 9 || m == 11) { return 30 }return 31}// 0 = Monday … 6 = Sunday (Sakamoto's method gives 0 = Sunday)weekdayOf(y, m, d) {t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]yy = m < 3 ? y - 1 : ysun = (yy + (yy - yy % 4) / 4 - (yy - yy % 100) / 100 + (yy - yy % 400) / 400 + t[m - 1] + d) % 7return (sun + 6) % 7}// "YYYY-MM-DD" → yyyymmdd as a number (strings have no ordering operators), or 0 when it is not a real daykeyOf(s) {if (s.length != 10 || s.slice(4, 5) != '-' || s.slice(7, 8) != '-') { return 0 }y = toNumber(s.slice(0, 4))m = toNumber(s.slice(5, 7))d = toNumber(s.slice(8, 10))if (y == null || m == null || d == null) { return 0 }if (m < 1 || m > 12 || d < 1 || d > daysIn(y, m)) { return 0 }if (('' + (10000 + y)).slice(1) + '-' + pad(m) + '-' + pad(d) != s) { return 0 }return y * 10000 + m * 100 + d}// 42 days (six weeks of seven) so the popup keeps its height; each carries its state as a classbuild(y, m, todayText, chosen) {py = m == 1 ? y - 1 : ypm = m == 1 ? 12 : m - 1ny = m == 12 ? y + 1 : ynm = m == 12 ? 1 : m + 1lead = weekdayOf(y, m, 1)let n = daysIn(y, m)pn = daysIn(py, pm)out = []let day = 1 - leadlet i = 0while (i < 42) {let cy = ylet cm = mlet cd = daylet cls = ''if (day < 1) { cy = py cm = pm cd = pn + day cls = 'outside' }if (day > n) { cy = ny cm = nm cd = day - n cls = 'outside' }let date = ('' + (10000 + cy)).slice(1) + '-' + pad(cm) + '-' + pad(cd)if (date == todayText) { cls = cls + ' today' }if (date == chosen) { cls = cls + ' selected' }out.push({ n = cd date = date cls = cls })day = day + 1i = i + 1}return out}show(y, m) {year = ymonth = mtitle = monthNames[m - 1] + ' ' + ydays = build(y, m, today, selected)}// the row of a field nameindexOf(name) {let i = 0while (i < fields.length) {if (fields[i] == name) { return i }i = i + 1}return 0}// the input element of a field namefield(name) { return document.querySelector('date-picker input[name="' + name + '"]') }// a field's text changed (typed or picked): keep it, mark it when it is not a real day, show its monthsetValue(name, text, showMonth) {i = indexOf(name)k = keyOf(text)values[i] = textel = field(name)if (el.value != text) { el.value = text }el.setAttribute('aria-invalid', text != '' && k == 0 ? 'true' : 'false')selected = k != 0 ? text : ''if (k != 0 && showMonth) { show(toNumber(text.slice(0, 4)), toNumber(text.slice(5, 7))) } else { show(year, month) }}// Look: black, white and the greys #555 #999 #ddd #f2f2f2 (the library's base, named in shared/base.hl; README "Look"). Every colour is a semantic token// with its base value as fallback, `var(--color-…, #000)`, so a bare copy looks right and an app that declares the tokens (layouts.worldapi.org's// theme) restyles it. No frames: a border or ring only where it means something (focus, today, an invalid field). A component's browser module// cannot import tokens itself.Style {'date-picker' { display = 'grid' gap = '.75rem' maxWidth = '18rem' }'date-field' { position = 'relative' display = 'block' }'date-picker input' { width = '100%' boxSizing = 'border-box' font = 'inherit' padding = '.4rem .6rem' color = 'var(--color-text, #000)' background = 'var(--color-background-sunken, #f2f2f2)' border = '0' borderRadius = '.4rem' }'date-picker input:focus' { outline = '2px solid var(--color-accent, #555)' outlineOffset = '-1px' }'date-picker input[aria-invalid="true"]' { outline = '2px dashed var(--color-danger, #000)' outlineOffset = '-2px' }'date-picker picker-popup' { display = 'none' position = 'absolute' top = '100%' left = '0' zIndex = '4' width = 'min(22rem, 90vw)' marginTop = '.25rem' outline = 'none' }'date-field:focus-within picker-popup' { display = 'block' }'date-picker month-view' { display = 'grid' gap = '.5rem' padding = '.5rem' background = 'var(--color-background, #fff)' border = '0' borderRadius = '.5rem' boxShadow = '0 .5rem 1.5rem rgba(0, 0, 0, .25)' }'date-picker month-view header' { display = 'flex' alignItems = 'center' justifyContent = 'space-between' gap = '.5rem' }'date-picker month-view h2' { margin = '0' fontSize = '1.1rem' color = 'var(--color-text-strong, #000)' textAlign = 'center' flex = '1' textTransform = 'capitalize' }'date-picker month-view ol' { display = 'grid' gridTemplateColumns = 'repeat(7, minmax(0, 1fr))' gap = '.2rem' margin = '0' padding = '0' listStyle = 'none' }'date-picker button' { font = 'inherit' fontWeight = '600' padding = '.4rem .9rem' color = 'var(--color-text, #000)' background = 'transparent' border = '0' borderRadius = '.4rem' cursor = 'pointer' }'date-picker button:hover' { background = 'var(--color-surface, #f2f2f2)' }'date-picker ol.weekdays li' { padding = '.3rem 0' fontSize = '.8rem' fontWeight = '600' color = 'var(--color-text-muted, #555)' textAlign = 'center' }'date-picker ol.days li' { minHeight = '2rem' padding = '.3rem' border = '0' borderRadius = '.3rem' textAlign = 'right' background = 'var(--color-surface, #f2f2f2)' cursor = 'pointer' }'date-picker ol.days li:hover' { background = 'var(--color-background-sunken, #ddd)' }'date-picker ol.days time' { color = 'var(--color-text, #000)' fontSize = '.95rem' }'date-picker ol.days li.outside' { background = 'var(--color-background, #fff)' }'date-picker ol.days li.outside time' { color = 'var(--color-text-muted, #555)' }'date-picker ol.days li.today' { outline = '2px solid var(--color-accent, #000)' outlineOffset = '-2px' }'date-picker ol.days li.selected' { background = 'var(--color-accent, #000)' }'date-picker ol.days li.selected time' { color = 'var(--color-accent-text, #fff)' }}View {datePicker {for (r of rows) {dateField {input { name = r.name type = "text" inputmode = "numeric" autocomplete = "off" maxlength = "10" title = r.hint placeholder = placeholder 'aria-invalid' = "false" on focus(e) { emit dateOpen(r.name) } on input(e) { emit dateTyped(r.name, e.target.value) } on keydown(e) { emit dateKey(e.key) } }pickerPopup { tabindex = "-1"monthView {header {button { type = "button" class = "quiet prev" title = prevLabel "‹" on click(e) { emit datePrev(e) } }h2 { title }button { type = "button" class = "quiet next" title = nextLabel "›" on click(e) { emit dateNext(e) } }}ol { class = "weekdays" for (wd of weekdays) { li { wd } } }ol { class = "days" for (d of days) { li { class = d.cls time { datetime = d.date d.n } on click(e) { emit datePick(r.name, d.date) } } } }}}}}}}// the browser knows today and its language; a field gets focus first, so this runs before a day is clickedon dateOpen(name) {let now = new Date()lang = navigator.language != null ? navigator.language : 'en't = ('' + (10000 + now.getFullYear())).slice(1) + '-' + pad(now.getMonth() + 1) + '-' + pad(now.getDate())if (t != today) {mf = Intl.DateTimeFormat(lang, { month = 'long' })df = Intl.DateTimeFormat(lang, { weekday = 'short' })ms = []ds = []let i = 0while (i < 12) { ms.push(mf.format(new Date(Date.UTC(2024, i, 15, 12)))) i = i + 1 }i = 0while (i < 7) { ds.push(df.format(new Date(Date.UTC(2024, 0, 1 + i, 12)))) i = i + 1 }today = tmonthNames = msweekdayNames = dsweekdays = dsde = lang.slice(0, 2) == 'de'prevLabel = de ? 'Voriger Monat' : 'Previous month'nextLabel = de ? 'Nächster Monat' : 'Next month'}text = values[indexOf(name)]selected = keyOf(text) != 0 ? text : ''if (selected != '') { show(toNumber(text.slice(0, 4)), toNumber(text.slice(5, 7))) } else { show(toNumber(today.slice(0, 4)), toNumber(today.slice(5, 7))) }}on dateTyped(name, text) { setValue(name, text, true) }on dateKey(key) {if (key == 'Escape' || key == 'Enter') { document.activeElement.blur() }}on datePrev(e) { show(month == 1 ? year - 1 : year, month == 1 ? 12 : month - 1) }on dateNext(e) { show(month == 12 ? year + 1 : year, month == 12 ? 1 : month + 1) }on datePick(name, d) {setValue(name, d, false)document.activeElement.blur()}
Branches
Latest commits
- d17f8543mission 012: re-vendor Hybriel master f9614007 (plugins), fixes comments push redraw; gate 480/0mre
- 5f95de05mission 012: re-vendor Hybriel ce6f2e0a (plugins), report: gate 478/2 on comments push redrawmre
- 3794c591mission 011: report, LOG, STATUS, md-keys-run widths, cdp-blank helpermre
- 77a93c16mission 011: gate hook for md-keys (480/0), flatten() runs one at a time (removeChild race), surface also built on first focus / pointer-down (background tab), md-bg test, README keysmre
- 3c906718mission 011: md-editor key tests (real keys) and fixes: nested p/ul flatten, bare br, link change, Ctrl+E, code block exit (WIP)mre
- 1442e583mission 010: report, LOG, STATUSmre
- 6f27ad31md-editor: phone toolbar row contained (inline-size) so it never widens a grid parentmre
- d702d76dmd-editor look: root class md-empty (a host .empty rule hit it), rows = surface height, one phone toolbar row with 44 px targets; comment form rows 3; gate 360/0mre
- 15be2ac0comments: attach (FilesUpload pick), onPost(text, files), files per comment; md-editor files=off; README, gate fixmre
- 1f8d79d6comments (mission 009 WIP, tickets#27): attachments — attach="true" puts the form in FilesUpload pick mode, onPost(text, files), files per comment (thumbnails open full size, file rows, no url = no link); MdEditor files="off"; demo with attachments; gate m009 checks (WIP: 356/1)mre
- 13389d67mission 008: report, LOG, STATUSmre
- ab47e4a2files-upload (mission 008, #18 for hycon#46): pick-first-send-later mode — host-owned list (files + onChange), slot for the host's input area, Ctrl+V paste (both modes), thumbnails, name+size rows, ✕ remove, maxSize per file, Camera on touch screens, 44 px targets; demo /files-upload/pick; gate 343/0 (19 new, 18 fail on the old code)mre
- 172e66bemission brief 008mre
- 9db927dfpaths: projects moved into /media/STORAGE/projects/WorldAPI/mre
- 00a95329components (mission 007): re-vendor Hybriel to the shared static build 0f396652 (plugins; bin/ git-ignored), gate 324/0 old and new, real-data copy unchanged, report 007, same-output.mjs ports overridablemre
- fe50766bmission brief 007mre
- 2dce51b8components (mission 006) 4/4: docs (README comments + gate 324, STATUS, LOG), report reports/006-comments.mdmre
- 9ec7273ecomponents layouts#2 (mission 006) 3/4: comments — each text is MdEditor readonly directly in its row (hl:web builds a composed child anew when a bound value changes, so the keyed one-row list was not needed); demo 'bob edits his comment elsewhere (a push)' + gate check that a text the host changes from outside is drawn anew; theme radii (var(--radius-small/large) with 5px/10px fallbacks: layouts' check-theme 0 problems with comments.hl copied in); README 'How it is built'; gate 324/0 (34 comments checks)mre
- fd57c18bcomponents layouts#2 (mission 006) 2/3: comments block (components/comments, demo /comments) — pure Hybriel: '1 comment' / 'n comments', avatar initial, bubble with name, time, 'edited' (<details> with the earlier versions), the text drawn by MdEditor readonly (one-row list keyed by id + text, so an edit redraws), like ♥ n (aria-pressed), Edit / Delete (asks first) only where author == user, the form below in MdEditor (Comment button or Ctrl+Enter; a fresh editor after a post). Host passes comments + user and hands onPost / onEdit / onDelete / onLike (hybriel#87); demo keeps a mock thread from lib/comments.hl in the page, switches alice / bob / nobody. Home entry, routes, map line, README; base-look gate covers comments; 33 gate checks with real clicks / keysmre
- 850a376bcomponents (mission 006) 1/3: MdEditor readonly = "true" — the Markdown shown formatted and nothing else (no toolbar, link form, file rows, placeholder, source view or switch; article contenteditable false, tabindex -1, role document; no field background or focus ring); the mount only loads the value. For the comments block, which draws every comment with the editor's own renderermre