Class: Dommy::MediaQueryList

Inherits:
Object
  • Object
show all
Includes:
Bridge::Methods, EventTarget
Defined in:
lib/dommy/media_query_list.rb

Overview

MediaQueryList — what window.matchMedia(query) returns.

matches evaluates the query against the window's media environment (viewport 1280x720 by default; see Window#resize_to). When the environment changes, the window notifies every list it handed out and a change event fires for those whose result flipped.

__test_set_matches__(bool) remains as a test seam: it forces the match state (overriding evaluation) and fires change — the surface libraries like Material-UI / Bootstrap / @testing-library consult.

Spec: https://drafts.csswg.org/cssom-view/#mediaquerylist

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Bridge::Methods

included

Methods included from EventTarget

#__dommy_dump_event_failure__, #__internal_deliver_event__, #__internal_process_event_handler_return__, #add_event_listener, capture_flag, #deliver_at, #dispatch_event, #event_name_from_on, #invoke_listener_isolated, js_truthy?, #on_handler, #remove_event_listener, #set_on_handler

Constructor Details

#initialize(window, query) ⇒ MediaQueryList

Returns a new instance of MediaQueryList.



23
24
25
26
27
28
29
30
# File 'lib/dommy/media_query_list.rb', line 23

def initialize(window, query)
  @window = window
  @media = query.to_s
  @forced = nil
  @onchange = nil
  @last_matches = evaluate
  window.__internal_register_media_query_list__(self) if window.respond_to?(:__internal_register_media_query_list__)
end

Instance Attribute Details

#mediaObject (readonly)

Returns the value of attribute media.



21
22
23
# File 'lib/dommy/media_query_list.rb', line 21

def media
  @media
end

Instance Method Details

#__internal_environment_changed__Object

Called by the window when the media environment changed (resize etc.). Fires change when the evaluated result flipped; a forced value wins.



65
66
67
68
69
70
71
72
73
74
# File 'lib/dommy/media_query_list.rb', line 65

def __internal_environment_changed__
  return unless @forced.nil?

  current = evaluate
  return if current == @last_matches

  @last_matches = current
  dispatch_change(current)
  nil
end

#__internal_event_parent__Object



124
125
126
# File 'lib/dommy/media_query_list.rb', line 124

def __internal_event_parent__
  nil
end

#__js_call__(method, args) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/dommy/media_query_list.rb', line 109

def __js_call__(method, args)
  case method
  when "addListener"
    add_listener(args[0])
  when "removeListener"
    remove_listener(args[0])
  when "addEventListener"
    add_event_listener(args[0], args[1], args[2])
  when "removeEventListener"
    remove_event_listener(args[0], args[1], args[2])
  when "dispatchEvent"
    dispatch_event(args[0])
  end
end

#__js_get__(key) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/dommy/media_query_list.rb', line 76

def __js_get__(key)
  case key
  when "media"
    @media
  when "matches"
    matches
  when "onchange"
    @onchange
  else
    Bridge::ABSENT
  end
end

#__js_set__(key, value) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dommy/media_query_list.rb', line 89

def __js_set__(key, value)
  case key
  when "onchange"
    remove_event_listener("change", @onchange) if @onchange
    @onchange = value
    add_event_listener("change", value) if value
  else
    return Bridge::UNHANDLED
  end

  nil
end

#__test_set_matches__(value) ⇒ Object

Test seam: force the match state (evaluation is bypassed from then on) and dispatch a change event so subscribers re-render.



55
56
57
58
59
60
61
# File 'lib/dommy/media_query_list.rb', line 55

def __test_set_matches__(value)
  return if matches == !!value

  @forced = !!value
  dispatch_change(@forced)
  nil
end

#add_listener(callback) ⇒ Object Also known as: addListener

Spec aliases for legacy support.



41
42
43
# File 'lib/dommy/media_query_list.rb', line 41

def add_listener(callback)
  add_event_listener("change", callback)
end

#matchesObject Also known as: matches?



32
33
34
35
36
# File 'lib/dommy/media_query_list.rb', line 32

def matches
  return @forced unless @forced.nil?

  @last_matches = evaluate
end

#remove_listener(callback) ⇒ Object Also known as: removeListener



47
48
49
# File 'lib/dommy/media_query_list.rb', line 47

def remove_listener(callback)
  remove_event_listener("change", callback)
end