gitoria

components

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commita92298dda92298ddRecord exact CSS and same-date proof after specificity reviewmrea92298dd/components/range-picker/range-picker.hl

11.0 KB

  1. // range-picker.hl — THE DATE RANGE PICKER, all Hybriel: one text field ("YYYY-MM-DD – YYYY-MM-DD", typing works) that opens a
  2. // month under it; a click on a day sets the start, a second click sets the end (an earlier second click swaps them), the
  3. // days between are highlighted. The month grid is built here (no Date arithmetic: plain integers, so the server render and
  4. // the browser agree); the browser's own today and language come in when the field gets focus (`Date` and `Intl` are
  5. // browser globals of View handlers). The popup is shown by CSS while the field or the popup has focus (:focus-within), so
  6. // no `if` re-creates the DOM. The field posts in a form by its `name`.
  7. import { now, timestamp } from 'hl:time'
  8. // the server's today (UTC) for the first render only; the browser corrects it when the field gets focus
  9. serverToday = () => { return timestamp(now()).slice(0, 10) }
  10. String name = 'range'
  11. String value = ''
  12. String kept = ''
  13. String pending = ''
  14. String start = ''
  15. String end = ''
  16. String invalidText = 'false'
  17. String today = serverToday()
  18. Number year = toNumber(today.slice(0, 4))
  19. Number month = toNumber(today.slice(5, 7))
  20. Hybrid monthNames = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
  21. Hybrid weekdayNames = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
  22. Hybrid weekdays = weekdayNames
  23. String title = monthNames[month - 1] + ' ' + year
  24. Hybrid days = build(year, month, today, '', '')
  25. String prevLabel = 'Previous month'
  26. String nextLabel = 'Next month'
  27. String placeholder = 'YYYY-MM-DD – YYYY-MM-DD'
  28. String sep = ' – '
  29. pad(n) { return n < 10 ? '0' + n : '' + n }
  30. leap(y) { return (y % 4 == 0 && y % 100 != 0) || y % 400 == 0 }
  31. daysIn(y, m) {
  32. if (m == 2) { return leap(y) ? 29 : 28 }
  33. if (m == 4 || m == 6 || m == 9 || m == 11) { return 30 }
  34. return 31
  35. }
  36. // 0 = Monday … 6 = Sunday (Sakamoto's method gives 0 = Sunday)
  37. weekdayOf(y, m, d) {
  38. t = [0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4]
  39. yy = m < 3 ? y - 1 : y
  40. sun = (yy + (yy - yy % 4) / 4 - (yy - yy % 100) / 100 + (yy - yy % 400) / 400 + t[m - 1] + d) % 7
  41. return (sun + 6) % 7
  42. }
  43. // "YYYY-MM-DD" → yyyymmdd as a number (strings have no ordering operators), or 0 when it is not a real day
  44. keyOf(s) {
  45. if (s.length != 10 || s.slice(4, 5) != '-' || s.slice(7, 8) != '-') { return 0 }
  46. y = toNumber(s.slice(0, 4))
  47. m = toNumber(s.slice(5, 7))
  48. d = toNumber(s.slice(8, 10))
  49. if (y == null || m == null || d == null) { return 0 }
  50. if (m < 1 || m > 12 || d < 1 || d > daysIn(y, m)) { return 0 }
  51. if (('' + (10000 + y)).slice(1) + '-' + pad(m) + '-' + pad(d) != s) { return 0 }
  52. return y * 10000 + m * 100 + d
  53. }
  54. // 42 days (six weeks of seven) so the popup keeps its height; each carries its state as a class
  55. build(y, m, todayText, a, b) {
  56. py = m == 1 ? y - 1 : y
  57. pm = m == 1 ? 12 : m - 1
  58. ny = m == 12 ? y + 1 : y
  59. nm = m == 12 ? 1 : m + 1
  60. lead = weekdayOf(y, m, 1)
  61. let n = daysIn(y, m)
  62. pn = daysIn(py, pm)
  63. ka = keyOf(a)
  64. kb = keyOf(b)
  65. out = []
  66. let day = 1 - lead
  67. let i = 0
  68. while (i < 42) {
  69. let cy = y
  70. let cm = m
  71. let cd = day
  72. let cls = ''
  73. if (day < 1) { cy = py cm = pm cd = pn + day cls = 'outside' }
  74. if (day > n) { cy = ny cm = nm cd = day - n cls = 'outside' }
  75. let date = ('' + (10000 + cy)).slice(1) + '-' + pad(cm) + '-' + pad(cd)
  76. let k = cy * 10000 + cm * 100 + cd
  77. if (date == todayText) { cls = cls + ' today' }
  78. if (ka != 0 && kb != 0 && k > ka && k < kb) { cls = cls + ' between' }
  79. if ((ka != 0 && k == ka) || (kb != 0 && k == kb)) { cls = cls + ' selected' }
  80. out.push({ n = cd date = date cls = cls })
  81. day = day + 1
  82. i = i + 1
  83. }
  84. return out
  85. }
  86. show(y, m) {
  87. year = y
  88. month = m
  89. title = monthNames[m - 1] + ' ' + y
  90. days = build(y, m, today, start, end)
  91. }
  92. // the field's text → sets start / end when it is two real days (either order), else marks the field
  93. apply(text) {
  94. n = text.length
  95. a = n >= 21 ? text.slice(0, 10) : ''
  96. b = n >= 21 ? text.slice(n - 10, n) : ''
  97. ka = keyOf(a)
  98. kb = keyOf(b)
  99. if (ka != 0 && kb != 0) {
  100. start = ka <= kb ? a : b
  101. end = ka <= kb ? b : a
  102. invalidText = 'false'
  103. return true
  104. }
  105. start = ''
  106. end = ''
  107. invalidText = text != '' ? 'true' : 'false'
  108. return false
  109. }
  110. // the month of a "YYYY-MM-DD" day, when it is not the month on show
  111. gotoDay(s) {
  112. if (keyOf(s) == 0) { return null }
  113. show(toNumber(s.slice(0, 4)), toNumber(s.slice(5, 7)))
  114. }
  115. // 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
  116. // 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
  117. // theme) restyles it. No frames: a border or ring only where it means something (focus, today, an invalid field). A component's browser module
  118. // cannot import tokens itself.
  119. Style {
  120. rangePicker {
  121. position = 'relative'
  122. display = 'block'
  123. maxWidth = '18rem'
  124. input {
  125. width = '100%'
  126. boxSizing = 'border-box'
  127. minHeight = '2.75rem'
  128. font = 'inherit'
  129. padding = '.4rem .6rem'
  130. color = 'var(--color-text, #000)'
  131. background = 'var(--color-background-sunken, #f2f2f2)'
  132. border = '0'
  133. borderRadius = '.4rem'
  134. focus {
  135. outline = '2px solid var(--color-accent, #555)'
  136. outlineOffset = '-1px'
  137. }
  138. }
  139. // State attribute assigned by the View; no compound-selector form.
  140. 'input[aria-invalid="true"]' {
  141. outline = '2px dashed var(--color-danger, #000)'
  142. outlineOffset = '-2px'
  143. }
  144. pickerPopup {
  145. display = 'none'
  146. position = 'absolute'
  147. top = '100%'
  148. left = '0'
  149. zIndex = '4'
  150. width = 'min(22rem, 90vw)'
  151. marginTop = '.25rem'
  152. outline = 'none'
  153. }
  154. focusWithin {
  155. pickerPopup { display = 'block' }
  156. }
  157. monthView {
  158. display = 'grid'
  159. gap = '.5rem'
  160. padding = '.5rem'
  161. background = 'var(--color-background, #fff)'
  162. border = '0'
  163. borderRadius = '.5rem'
  164. boxShadow = '0 .5rem 1.5rem rgba(0, 0, 0, .25)'
  165. header {
  166. display = 'flex'
  167. alignItems = 'center'
  168. justifyContent = 'space-between'
  169. gap = '.5rem'
  170. }
  171. h2 {
  172. margin = '0'
  173. fontSize = '1.1rem'
  174. color = 'var(--color-text-strong, #000)'
  175. textAlign = 'center'
  176. flex = '1'
  177. textTransform = 'capitalize'
  178. }
  179. ol {
  180. display = 'grid'
  181. gridTemplateColumns = 'repeat(7, minmax(0, 1fr))'
  182. gap = '.2rem'
  183. margin = '0'
  184. padding = '0'
  185. listStyle = 'none'
  186. }
  187. }
  188. button {
  189. font = 'inherit'
  190. fontWeight = '600'
  191. minWidth = '2.75rem'
  192. minHeight = '2.75rem'
  193. padding = '.4rem .9rem'
  194. color = 'var(--color-text, #000)'
  195. background = 'transparent'
  196. border = '0'
  197. borderRadius = '.4rem'
  198. cursor = 'pointer'
  199. hover { background = 'var(--color-surface, #f2f2f2)' }
  200. }
  201. // Class or attribute assigned by the View or code; no compound-selector form.
  202. 'ol.weekdays' {
  203. li {
  204. padding = '.3rem 0'
  205. fontSize = '.8rem'
  206. fontWeight = '600'
  207. color = 'var(--color-text-muted, #555)'
  208. textAlign = 'center'
  209. }
  210. }
  211. // Class or attribute assigned by the View or code; no compound-selector form.
  212. 'ol.days' {
  213. li {
  214. minHeight = '2.75rem'
  215. padding = '.3rem'
  216. border = '0'
  217. borderRadius = '.3rem'
  218. textAlign = 'right'
  219. background = 'var(--color-surface, #f2f2f2)'
  220. cursor = 'pointer'
  221. hover { background = 'var(--color-background-sunken, #ddd)' }
  222. }
  223. time {
  224. color = 'var(--color-text, #000)'
  225. fontSize = '.95rem'
  226. }
  227. // Class or attribute assigned by the View or code; no compound-selector form.
  228. 'li.outside' {
  229. background = 'var(--color-background, #fff)'
  230. time { color = 'var(--color-text-muted, #555)' }
  231. }
  232. // Class or attribute assigned by the View or code; no compound-selector form.
  233. 'li.today' {
  234. outline = '2px solid var(--color-accent, #000)'
  235. outlineOffset = '-2px'
  236. }
  237. // Class or attribute assigned by the View or code; no compound-selector form.
  238. 'li.between' { background = 'var(--color-background-sunken, #ddd)' }
  239. // Class or attribute assigned by the View or code; no compound-selector form.
  240. 'li.selected' {
  241. background = 'var(--color-accent, #000)'
  242. time { color = 'var(--color-accent-text, #fff)' }
  243. }
  244. }
  245. }
  246. }
  247. View {
  248. rangePicker {
  249. input { id = "range-picker-input" name = name type = "text" autocomplete = "off" maxlength = "23" title = placeholder placeholder = placeholder value = value 'aria-invalid' = invalidText on focus(e) { emit rangeOpen(e) } on input(e) { emit rangeTyped(e.target.value) } on keydown(e) { emit rangeKey(e.key) } }
  250. pickerPopup { tabindex = "-1"
  251. monthView {
  252. header {
  253. button { id = "range-prev" type = "button" class = "quiet" title = prevLabel "‹" on click(e) { emit rangePrev(e) } }
  254. h2 { title }
  255. button { id = "range-next" type = "button" class = "quiet" title = nextLabel "›" on click(e) { emit rangeNext(e) } }
  256. }
  257. ol { class = "weekdays" for (wd of weekdays) { li { wd } } }
  258. ol { class = "days" for (d of days) { li { class = d.cls time { datetime = d.date d.n } on click(e) { emit rangePick(d.date) } } } }
  259. }
  260. }
  261. }
  262. }
  263. // the browser knows today; the names follow the page language (<html lang>, English when none); the field gets focus first, so this runs before a day is clicked
  264. on rangeOpen(e) {
  265. let now = new Date()
  266. lang = document.documentElement.lang != '' ? document.documentElement.lang : 'en'
  267. t = ('' + (10000 + now.getFullYear())).slice(1) + '-' + pad(now.getMonth() + 1) + '-' + pad(now.getDate())
  268. if (t != today || lang.slice(0, 2) != 'en') {
  269. mf = Intl.DateTimeFormat(lang, { month = 'long' timeZone = 'UTC' })
  270. df = Intl.DateTimeFormat(lang, { weekday = 'short' timeZone = 'UTC' })
  271. ms = []
  272. ds = []
  273. let i = 0
  274. while (i < 12) { ms.push(mf.format(new Date(Date.UTC(2024, i, 15, 12)))) i = i + 1 }
  275. i = 0
  276. while (i < 7) { ds.push(df.format(new Date(Date.UTC(2024, 0, 1 + i, 12)))) i = i + 1 }
  277. today = t
  278. monthNames = ms
  279. weekdayNames = ds
  280. weekdays = ds
  281. de = lang.slice(0, 2) == 'de'
  282. prevLabel = de ? 'Voriger Monat' : 'Previous month'
  283. nextLabel = de ? 'Nächster Monat' : 'Next month'
  284. }
  285. // a half-picked start is dropped; the month shown is the range's start, else today's month
  286. if (pending != '') {
  287. pending = ''
  288. value = kept
  289. apply(kept)
  290. }
  291. if (start != '') { gotoDay(start) } else { show(toNumber(today.slice(0, 4)), toNumber(today.slice(5, 7))) }
  292. }
  293. on rangeTyped(text) {
  294. pending = ''
  295. value = text
  296. if (apply(text)) { gotoDay(start) } else { show(year, month) }
  297. kept = start != '' ? start + sep + end : ''
  298. }
  299. on rangeKey(key) {
  300. if (key == 'Escape' || key == 'Enter') {
  301. if (pending != '') {
  302. pending = ''
  303. value = kept
  304. apply(kept)
  305. show(year, month)
  306. }
  307. document.activeElement.blur()
  308. }
  309. }
  310. on rangePrev(e) { show(month == 1 ? year - 1 : year, month == 1 ? 12 : month - 1) }
  311. on rangeNext(e) { show(month == 12 ? year + 1 : year, month == 12 ? 1 : month + 1) }
  312. on rangePick(d) {
  313. if (pending == '') {
  314. kept = start != '' ? start + sep + end : ''
  315. pending = d
  316. start = d
  317. end = ''
  318. value = d + sep
  319. invalidText = 'false'
  320. show(year, month)
  321. return null
  322. }
  323. a = keyOf(pending) <= keyOf(d) ? pending : d
  324. b = keyOf(pending) <= keyOf(d) ? d : pending
  325. pending = ''
  326. start = a
  327. end = b
  328. value = a + sep + b
  329. kept = value
  330. show(year, month)
  331. document.activeElement.blur()
  332. }

Branches

Latest commits

  • a92298ddRecord exact CSS and same-date proof after specificity reviewmre
  • c71a0abePreserve consumer theme specificity with nested tag rulesmre
  • 64a2bc27Document CS23b style formatting proof and review statusmre
  • a72b57cficons: nest Style rules in the language's formmre
  • 927616e2pagination: nest Style rules in the language's formmre
  • aab8ad29code-editor: nest Style rules in the language's formmre
  • ba30727afile-upload: nest Style rules in the language's formmre
  • 42fefa4emodal: nest Style rules in the language's formmre
  • 6fd953e3range-picker: nest Style rules in the language's formmre
  • f661951ddate-picker: nest Style rules in the language's formmre
  • 363577e6media-player: nest Style rules in the language's formmre
  • 0b3fd8c9comments: nest Style rules in the language's formmre
  • 67e043c8plugins/web/README.md from hybriel cb9e480dmre
  • 519b56e9styles.hl: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
  • 29f63d01md-editor: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
  • 869f49c7files-upload: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
  • f60dab20month-view: Style in the language's form (nested like the View, one declaration per line, bare @media)mre
  • 5b3c349aRe-vendor to Hybriel set day35 (cb9e480d): plugins core and web sources, README vendoring notemre
  • 3449b117components: a startup error prints its message before exit 1 (hybriel#247)mre
  • 59704ce1Merge branch 'n5b-pipes'mre