phlex_kit 0.14.1 → 0.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cc498540064c2a8e95dbe9b86dad991b2decb5dbd36c58f8ea8c39dce04fd146
4
- data.tar.gz: 4b04765fe0ef28d0562542546df1d841fe2a9bd0a6df3cde2c344a5f80902d98
3
+ metadata.gz: 7e817130c54d3e6a8c547328c09b8e632e13adda0cafa7ed0f415359fd1c0ed9
4
+ data.tar.gz: bcb04540a46d3442ac21b8d54d10b1f903bbcf33af2ec65344e7ad6879ab8462
5
5
  SHA512:
6
- metadata.gz: f94e22d2a6537aae8571df45dea339fe6f03c2ed64b084223ee9359240f4032c85c1ce2132b63321e9d334b99cd9e0619babe7ec390937446913759434ae2a6c
7
- data.tar.gz: e8a31936f3a6bd52fa30b61f3faf36e0ac8845ad78fd5c10624a021f3c0c6e6e99124eab540e42e0a0fd0dbe99bb4e6e2e2845c978939f646755a92e0576e30e
6
+ metadata.gz: ad56db4fec9925c964ed13084658ddf29942d79d17d4cd141bdb3efa0bef5d3e0be0c6c9ff8f53b28da163dfb0f080c1a79c8330b806f01823e0e092b479a5f5
7
+ data.tar.gz: 3e90c49dc0cb097b2aee66a28d7537c7c834df03015338832e6f00c9cbe40c93374d7a1d034a3c9765b848dc12407afc44681d2b6ef4aa62176295fecb1f3ad2
@@ -16,7 +16,11 @@ module PhlexKit
16
16
  # the year list). Pass `input_id:` a CSS selector (e.g. "#due-date") to
17
17
  # push the picked date (or "start – end" range) into a
18
18
  # phlex-kit--calendar-input outlet — that's how PhlexKit::DatePicker binds
19
- # an Input. Sizing rides --pk-cell-size/--pk-cell-radius (calendar.css).
19
+ # an Input. `locale:` (a BCP 47 tag) drives every Intl call the controller
20
+ # makes — caption, weekday headers, month dropdown, day accessible names and
21
+ # the EEEE/MMMM format tokens; unset follows the browser. The server-rendered
22
+ # month/weekday words stay English as the pre-JS baseline. Note the `do`/PPPP
23
+ # ordinal suffixes (st/nd/rd/th) are an English rule and stay English. Sizing rides --pk-cell-size/--pk-cell-radius (calendar.css).
20
24
  class Calendar < BaseComponent
21
25
  MODES = { single: "single", range: "range", multiple: "multiple" }.freeze
22
26
  CAPTION_LAYOUTS = %i[label dropdown].freeze
@@ -25,7 +29,7 @@ module PhlexKit
25
29
  def initialize(mode: :single, selected_date: nil, selected_dates: [], range_start: nil, range_end: nil,
26
30
  min_date: nil, max_date: nil, disabled_dates: [], week_numbers: false,
27
31
  caption_layout: :label, from_year: nil, to_year: nil,
28
- input_id: nil, date_format: "yyyy-MM-dd", **attrs)
32
+ input_id: nil, date_format: "yyyy-MM-dd", locale: nil, **attrs)
29
33
  @mode = MODES.fetch(mode.to_sym)
30
34
  @selected_date = selected_date
31
35
  @selected_dates = Array(selected_dates).map(&:to_s)
@@ -41,6 +45,7 @@ module PhlexKit
41
45
  @to_year = to_year
42
46
  @input_id = input_id
43
47
  @date_format = date_format
48
+ @locale = locale
44
49
  @attrs = attrs
45
50
  end
46
51
 
@@ -112,6 +117,8 @@ module PhlexKit
112
117
  data[:phlex_kit__calendar_selected_dates_value] = JSON.generate(@selected_dates) if @selected_dates.any?
113
118
  data[:phlex_kit__calendar_disabled_dates_value] = JSON.generate(@disabled_dates) if @disabled_dates.any?
114
119
  data[:phlex_kit__calendar_week_numbers_value] = "true" if @week_numbers
120
+ # Unset = the runtime's own locale (the controller passes undefined to Intl).
121
+ data[:phlex_kit__calendar_locale_value] = @locale if @locale
115
122
  # Seed the view on the selection so the grid opens on the right month.
116
123
  view_seed = @selected_date || @range_start || @selected_dates.first
117
124
  data[:phlex_kit__calendar_view_date_value] = view_seed.to_s if view_seed
@@ -41,6 +41,10 @@ export default class extends Controller {
41
41
  // toISOString() is UTC (wrong day for non-UTC users near midnight).
42
42
  viewDate: { type: String, default: "" },
43
43
  format: { type: String, default: "yyyy-MM-dd" },
44
+ // BCP 47 tag for every Intl call below. Unset = "" = undefined to Intl,
45
+ // i.e. the runtime's own locale (the server markup is English only as the
46
+ // pre-JS baseline, so the grid localizes itself on first render).
47
+ locale: { type: String, default: "" },
44
48
  };
45
49
  static outlets = ["phlex-kit--calendar-input"];
46
50
 
@@ -274,6 +278,34 @@ export default class extends Controller {
274
278
  // `focusDay` overrides the day to refocus after re-render: on a keyboard
275
279
  // month cross the target day lives in the NEW grid, not the pre-render
276
280
  // activeElement, so onKeydown passes it explicitly.
281
+ // undefined (not "") is what Intl wants for "use the runtime default".
282
+ locale() {
283
+ return this.localeValue || undefined;
284
+ }
285
+
286
+ // The weekday <th>s are cloned verbatim from a server template and the month
287
+ // dropdown's <option>s are server-rendered — both English. Relabel them from
288
+ // Intl on every render so the whole chrome speaks one language.
289
+ localizeChrome() {
290
+ const heads = [...this.calendarTarget.querySelectorAll(".pk-calendar-weekday")].filter(
291
+ (th) => !th.classList.contains("pk-calendar-weeknumber-head"),
292
+ );
293
+ // The server row runs Monday..Sunday; 2024-01-01 was a Monday.
294
+ heads.forEach((th, index) => {
295
+ const day = new Date(2024, 0, 1 + index);
296
+ const name = day.toLocaleDateString(this.locale(), { weekday: "long" });
297
+ th.setAttribute("aria-label", name);
298
+ th.textContent = day.toLocaleDateString(this.locale(), { weekday: "short" }).slice(0, 2);
299
+ });
300
+
301
+ if (!this.hasMonthSelectTarget) return;
302
+ [...this.monthSelectTarget.options].forEach((option) => {
303
+ const month = Number(option.value);
304
+ if (!Number.isFinite(month)) return;
305
+ option.textContent = new Date(2024, month, 1).toLocaleDateString(this.locale(), { month: "long" });
306
+ });
307
+ }
308
+
277
309
  updateCalendar(focusDay = null) {
278
310
  if (this.hasTitleTarget) {
279
311
  this.titleTarget.textContent = this.monthAndYear();
@@ -288,6 +320,7 @@ export default class extends Controller {
288
320
  ? document.activeElement.dataset?.day
289
321
  : null);
290
322
  this.calendarTarget.innerHTML = this.calendarHTML();
323
+ this.localizeChrome();
291
324
  this.ensureGridTabStop(focusedDay);
292
325
  }
293
326
 
@@ -419,7 +452,7 @@ export default class extends Controller {
419
452
  dayDate: day.getDate(),
420
453
  // full human-readable date for the button's accessible name — a bare
421
454
  // day number ("14") is meaningless to a screen reader
422
- dayLabel: day.toLocaleDateString("en-US", { weekday: "long", month: "long", day: "numeric", year: "numeric" }),
455
+ dayLabel: day.toLocaleDateString(this.locale(), { weekday: "long", month: "long", day: "numeric", year: "numeric" }),
423
456
  state: this.dayState(day),
424
457
  };
425
458
 
@@ -499,10 +532,10 @@ export default class extends Controller {
499
532
  .filter(Boolean);
500
533
  }
501
534
 
535
+ // One Intl call rather than `${month} ${year}` so locales that order the
536
+ // parts differently (ja-JP → "2026年1月") come out right, not just translated.
502
537
  monthAndYear() {
503
- const month = this.viewDate().toLocaleString("en-US", { month: "long" });
504
- const year = this.viewDate().getFullYear();
505
- return `${month} ${year}`;
538
+ return this.viewDate().toLocaleDateString(this.locale(), { month: "long", year: "numeric" });
506
539
  }
507
540
 
508
541
  selectedDate() {
@@ -592,8 +625,8 @@ export default class extends Controller {
592
625
  const hours = date.getHours();
593
626
  const minutes = date.getMinutes();
594
627
  const seconds = date.getSeconds();
595
- const dayOfWeek = date.toLocaleString("en-US", { weekday: "long" });
596
- const monthName = date.toLocaleString("en-US", { month: "long" });
628
+ const dayOfWeek = date.toLocaleString(this.locale(), { weekday: "long" });
629
+ const monthName = date.toLocaleString(this.locale(), { month: "long" });
597
630
  const daySuffix = this.getDaySuffix(day);
598
631
 
599
632
  const map = {
@@ -7,7 +7,15 @@ module PhlexKit
7
7
  # Upstream's fuse.js fuzzy search is replaced with a dependency-free substring
8
8
  # match in the controller. Tailwind → vanilla `.pk-command*` (command.css).
9
9
  class Command < BaseComponent
10
- def initialize(**attrs)
10
+ # Announcement templates for the live region — the only strings the
11
+ # controller writes itself (CommandEmpty is server-rendered, so it
12
+ # localizes with the rest of your markup). %{count} is interpolated.
13
+ DEFAULT_RESULTS_FORMAT = "%{count} result(s)"
14
+ DEFAULT_NO_RESULTS_TEXT = "No results"
15
+
16
+ def initialize(results_format: DEFAULT_RESULTS_FORMAT, no_results_text: DEFAULT_NO_RESULTS_TEXT, **attrs)
17
+ @results_format = results_format
18
+ @no_results_text = no_results_text
11
19
  @attrs = attrs
12
20
  end
13
21
 
@@ -21,12 +29,17 @@ module PhlexKit
21
29
  private
22
30
 
23
31
  # Screen-reader announcement of the filtered result count — the controller
24
- # writes "N results" / "No results" into it from filter().
32
+ # writes the results_format / no_results_text strings into it from
33
+ # filter(), interpolating %{count}.
25
34
  def live_region
26
35
  div(
27
36
  class: "pk-sr-only",
28
37
  aria: { live: "polite" },
29
- data: { phlex_kit__command_target: "liveRegion" }
38
+ data: {
39
+ phlex_kit__command_target: "liveRegion",
40
+ results_format: @results_format,
41
+ no_results_text: @no_results_text
42
+ }
30
43
  )
31
44
  end
32
45
  end
@@ -262,8 +262,13 @@ export default class extends Controller {
262
262
  if (count === null) {
263
263
  this.liveRegionTarget.textContent = "";
264
264
  } else {
265
+ // Templates come from the server (Command's results_format: /
266
+ // no_results_text: kwargs) — the defaults mirror command.rb's.
267
+ const data = this.liveRegionTarget.dataset;
265
268
  this.liveRegionTarget.textContent =
266
- count === 0 ? "No results" : `${count} result${count === 1 ? "" : "s"}`;
269
+ count === 0
270
+ ? data.noResultsText || "No results"
271
+ : (data.resultsFormat || "%{count} result(s)").replace("%{count}", count);
267
272
  }
268
273
  }
269
274
 
@@ -20,6 +20,7 @@ module PhlexKit
20
20
  placeholder: "Select a date",
21
21
  selected_date: value,
22
22
  date_format: "yyyy-MM-dd",
23
+ locale: nil,
23
24
  input_attrs: {},
24
25
  calendar_attrs: {},
25
26
  trigger_attrs: {},
@@ -31,6 +32,7 @@ module PhlexKit
31
32
  @label = label
32
33
  @selected_date = selected_date
33
34
  @date_format = date_format
35
+ @locale = locale
34
36
  # Seed the input with the same date_format the calendar controller
35
37
  # writes — a bare `selected_date.to_s` (ISO) would mismatch the format
36
38
  # until the first interaction.
@@ -65,7 +67,8 @@ module PhlexKit
65
67
  end
66
68
  end
67
69
  render PopoverContent.new(**@content_attrs) do
68
- render Calendar.new(input_id: input_selector, selected_date: @selected_date, date_format: @date_format, **@calendar_attrs)
70
+ render Calendar.new(input_id: input_selector, selected_date: @selected_date, date_format: @date_format,
71
+ locale: @locale, **@calendar_attrs)
69
72
  end
70
73
  end
71
74
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PhlexKit
4
- VERSION = "0.14.1"
4
+ VERSION = "0.15.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phlex_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.1
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Kennedy