Skip to content

Commit b7c6e05

Browse files
authored
Merge pull request #198 from JJJ/jjj/mobile-fullscreen
Mobile: Add an opt-in full-screen phone picker
2 parents 9b62a22 + 80b7de2 commit b7c6e05

56 files changed

Lines changed: 1489 additions & 36 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎CHANGELOG.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This project is a continuation and modernization of the original [harvesthq/chos
1212
- Give Vanilla selects marked `aria-invalid="true"` the same customizable invalid border as classic and React Chosen. The generated host now gains `chosen-native--invalid`; this class is a new styling hook for integrations.
1313

1414
### Added
15+
- Add opt-in `mobile_fullscreen` for a full-screen picker on narrow touch devices across jQuery, Prototype, Vanilla, and React (harvesthq/chosen#2438). React uses `mobileFullscreen`; desktop and default mobile dropdowns are unchanged.
1516
- Add opt-in `shift_select_range` to select a visible range in multiple selects with Shift-click across jQuery, Prototype, Vanilla, and React (harvesthq/chosen#135). React uses `shiftSelectRange`. Disabled options and selection limits still apply.
1617
- Add opt-in `placeholder_text_multiple_selected` to show an “Add another...” style search hint after the first multiple-selection choice across jQuery, Prototype, Vanilla, and React (harvesthq/chosen#171). React uses `placeholderTextMultipleSelected`.
1718
- Add opt-in `inherit_optgroup_classes` to copy parent optgroup classes onto selected multiple-choice chips across jQuery, Prototype, Vanilla, and React, so applications can style selections by group (harvesthq/chosen#572). React uses `inheritOptgroupClasses`.
@@ -123,6 +124,7 @@ This project is a continuation and modernization of the original [harvesthq/chos
123124
- Document Chosen's main CSS selectors and container states, and correct the generated ID in the arrow and height example.
124125

125126
### Developer notes
127+
- Opting into `mobile_fullscreen` adds a Close button to each generated container and applies `chosen-mobile-fullscreen`, `chosen-native--mobile-fullscreen`, or `chosen-react--mobile-fullscreen` while open on a qualifying touch screen. Integrations that inspect generated children or style open containers should allow this opt-in markup and class.
126128
- With opt-in `placeholder_text_multiple_selected`, the generated multiple-search input's `placeholder` attribute changes after the first selection. The source select and submitted values are unchanged; the default generated attribute remains unchanged.
127129
- Enabling `inherit_optgroup_classes` adds the source optgroup's classes to generated selected-choice chips. Default classes and markup remain unchanged; selectors targeting those classes may now also match chips when the option is enabled.
128130
- With opt-in `no_results_template`, the classic no-results row moves its search-term `<span>` to the `{search}` marker, or omits it when the marker is absent. Vanilla and React likewise add a `<span>` at the marker. The default generated markup is unchanged.

‎coffee/chosen.jquery.coffee‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ class Chosen extends AbstractChosen
159159
@selected_item.on 'keydown.chosen', (evt) => this.selected_item_keydown(evt); return
160160

161161
destroy: ->
162+
this.hide_mobile_fullscreen()
162163
this.cancel_pending_search()
163164
clearTimeout(@form_reset_timeout) if @form_reset_timeout?
164165
$(window).off 'pageshow.chosen', @pageshow_handler
@@ -408,13 +409,14 @@ class Chosen extends AbstractChosen
408409
@form_field_jq.trigger("chosen:maxselected", {chosen: this})
409410
return false
410411

411-
if this.should_dropup()
412+
if not this.mobile_fullscreen_matches() and this.should_dropup()
412413
@container.addClass "chosen-dropup"
413414

414415
@container.addClass "chosen-with-drop"
415416
@selected_item.attr("aria-expanded", true) unless @is_multiple
416417
@dropdown.attr("aria-hidden", false)
417418
@results_showing = true
419+
this.show_mobile_fullscreen()
418420

419421
@search_field.attr("aria-expanded", true)
420422
@search_field.trigger "focus"
@@ -440,6 +442,7 @@ class Chosen extends AbstractChosen
440442
@form_field_jq.trigger("chosen:search_updated", {chosen: this, search_term: search_term})
441443

442444
results_hide: ->
445+
this.hide_mobile_fullscreen()
443446
this.cancel_pending_search()
444447
this.clear_typeahead()
445448
if @results_showing

‎coffee/chosen.proto.coffee‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ class @Chosen extends AbstractChosen
143143
@selected_item.observe "keydown", (evt) => this.selected_item_keydown(evt)
144144

145145
destroy: ->
146+
this.hide_mobile_fullscreen()
146147
this.cancel_pending_search()
147148
clearTimeout(@form_reset_timeout) if @form_reset_timeout?
148149
Event.stopObserving window, 'pageshow', @pageshow_handler
@@ -412,13 +413,14 @@ class @Chosen extends AbstractChosen
412413
@form_field.fire("chosen:maxselected", { chosen: this })
413414
return false
414415

415-
if this.should_dropup()
416+
if not this.mobile_fullscreen_matches() and this.should_dropup()
416417
@container.addClassName "chosen-dropup"
417418

418419
@container.addClassName "chosen-with-drop"
419420
@selected_item.writeAttribute("aria-expanded", "true") unless @is_multiple
420421
@dropdown.writeAttribute("aria-hidden", "false")
421422
@results_showing = true
423+
this.show_mobile_fullscreen()
422424

423425
@search_field.writeAttribute("aria-expanded", "true")
424426
@search_field.focus()
@@ -447,6 +449,7 @@ class @Chosen extends AbstractChosen
447449
@form_field.fire("chosen:search_updated", {chosen: this, search_term: search_term})
448450

449451
results_hide: ->
452+
this.hide_mobile_fullscreen()
450453
this.cancel_pending_search()
451454
this.clear_typeahead()
452455
if @results_showing

‎coffee/lib/abstract-chosen.coffee‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class AbstractChosen
3030
this.setup()
3131

3232
this.set_up_html()
33+
this.setup_mobile_fullscreen()
3334
this.register_observers()
3435
this.transfer_focus() if form_field_had_focus
3536
# instantiation done, fire ready
@@ -77,6 +78,55 @@ class AbstractChosen
7778
else
7879
(@selected_item[0] or @selected_item).focus()
7980

81+
setup_mobile_fullscreen: ->
82+
return unless @mobile_fullscreen
83+
container = @container[0] or @container
84+
@mobile_close_button = document.createElement('button')
85+
@mobile_close_button.type = 'button'
86+
@mobile_close_button.className = 'chosen-mobile-close'
87+
@mobile_close_button.textContent = 'Close'
88+
@mobile_close_button.setAttribute('aria-label', 'Close options')
89+
for event_name in ['mousedown', 'touchstart']
90+
@mobile_close_button.addEventListener event_name, (evt) -> evt.stopPropagation()
91+
@mobile_close_button.addEventListener 'click', (evt) =>
92+
evt.preventDefault()
93+
evt.stopPropagation()
94+
this.close_field()
95+
container.appendChild(@mobile_close_button)
96+
97+
mobile_fullscreen_matches: ->
98+
@mobile_fullscreen and window.matchMedia? and window.matchMedia('(max-width: 600px) and (pointer: coarse)').matches
99+
100+
show_mobile_fullscreen: ->
101+
return unless this.mobile_fullscreen_matches()
102+
container = @container[0] or @container
103+
container.classList.add('chosen-mobile-fullscreen')
104+
lock = document.body.__chosenMobileFullscreenLock ?= {count: 0, overflow: document.body.style.overflow}
105+
lock.count++
106+
document.body.style.overflow = 'hidden'
107+
@mobile_viewport_sync = =>
108+
viewport = window.visualViewport
109+
container.style.setProperty('--chosen-mobile-top', "#{viewport?.offsetTop or 0}px")
110+
container.style.setProperty('--chosen-mobile-left', "#{viewport?.offsetLeft or 0}px")
111+
container.style.setProperty('--chosen-mobile-width', "#{viewport?.width or window.innerWidth}px")
112+
container.style.setProperty('--chosen-mobile-height', "#{viewport?.height or window.innerHeight}px")
113+
@mobile_viewport_sync()
114+
window.visualViewport?.addEventListener('resize', @mobile_viewport_sync)
115+
window.visualViewport?.addEventListener('scroll', @mobile_viewport_sync)
116+
117+
hide_mobile_fullscreen: ->
118+
container = @container?[0] or @container
119+
return unless container?.classList.contains('chosen-mobile-fullscreen')
120+
window.visualViewport?.removeEventListener('resize', @mobile_viewport_sync)
121+
window.visualViewport?.removeEventListener('scroll', @mobile_viewport_sync)
122+
lock = document.body.__chosenMobileFullscreenLock
123+
if lock? and --lock.count is 0
124+
document.body.style.overflow = lock.overflow
125+
delete document.body.__chosenMobileFullscreenLock
126+
container.classList.remove('chosen-mobile-fullscreen')
127+
for property in ['--chosen-mobile-top', '--chosen-mobile-left', '--chosen-mobile-width', '--chosen-mobile-height']
128+
container.style.removeProperty(property)
129+
80130
set_default_values: ->
81131
@click_test_action = (evt) => this.test_active_click(evt)
82132
@activate_action = (evt) => this.activate_field(evt)
@@ -102,6 +152,7 @@ class AbstractChosen
102152
@search_matcher = if typeof @options.search_matcher is "function" then @options.search_matcher else null
103153
@search_input_type = if @options.search_input_type is "text" then "text" else "search"
104154
@fixed_dropdown = @options.dropdown_position is "fixed"
155+
@mobile_fullscreen = @options.mobile_fullscreen is true
105156
@recalculate_width_on_update = @options.recalculate_width_on_update || false
106157
@split_search_terms = @options.split_search_terms || false
107158
@paste_multiple_values = @options.paste_multiple_values is true
@@ -1274,6 +1325,7 @@ class AbstractChosen
12741325
include_group_label_in_selected: 'boolean'
12751326
case_sensitive_search: 'boolean'
12761327
hide_results_on_select: 'boolean'
1328+
mobile_fullscreen: 'boolean'
12771329
rtl: 'boolean'
12781330
disable_search_threshold: 'integer'
12791331
max_selected_options: 'integer'

‎dist/css/chosen.css‎

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/css/chosen.css.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/css/chosen.min.css‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)