adapter.d.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /**
  2. * @license
  3. * Copyright 2020 Google Inc.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. import { EventType, SpecificEventListener } from '@material/base/types';
  24. import { Thumb, TickMark } from './types';
  25. /**
  26. * Defines the shape of the adapter expected by the foundation.
  27. * Implement this adapter for your framework of choice to delegate updates to
  28. * the component in your framework of choice. See architecture documentation
  29. * for more details.
  30. * https://github.com/material-components/material-components-web/blob/master/docs/code/architecture.md
  31. */
  32. export interface MDCSliderAdapter {
  33. /**
  34. * @return Returns true if the slider root element has the given class.
  35. */
  36. hasClass(className: string): boolean;
  37. /**
  38. * Adds the given class to the slider root element.
  39. */
  40. addClass(className: string): void;
  41. /**
  42. * Removes the given class from the slider root element.
  43. */
  44. removeClass(className: string): void;
  45. /**
  46. * @return Returns the given attribute value on the slider root element.
  47. */
  48. getAttribute(attribute: string): string | null;
  49. /**
  50. * Adds the class to the given thumb element.
  51. */
  52. addThumbClass(className: string, thumb: Thumb): void;
  53. /**
  54. * Removes the class from the given thumb element.
  55. */
  56. removeThumbClass(className: string, thumb: Thumb): void;
  57. /**
  58. * - If thumb is `Thumb.START`, returns the value property on the start input
  59. * (for range slider variant).
  60. * - If thumb is `Thumb.END`, returns the value property on the end input (or
  61. * only input for single point slider).
  62. */
  63. getInputValue(thumb: Thumb): string;
  64. /**
  65. * - If thumb is `Thumb.START`, sets the value property on the start input
  66. * (for range slider variant).
  67. * - If thumb is `Thumb.END`, sets the value property on the end input (or
  68. * only input for single point slider).
  69. */
  70. setInputValue(value: string, thumb: Thumb): void;
  71. /**
  72. * - If thumb is `Thumb.START`, returns the attribute value on the start input
  73. * (for range slider variant).
  74. * - If thumb is `Thumb.END`, returns the attribute value on the end input (or
  75. * only input for single point slider).
  76. */
  77. getInputAttribute(attribute: string, thumb: Thumb): string | null;
  78. /**
  79. * - If thumb is `Thumb.START`, sets the attribute on the start input
  80. * (for range slider variant).
  81. * - If thumb is `Thumb.END`, sets the attribute on the end input (or
  82. * only input for single point slider).
  83. */
  84. setInputAttribute(attribute: string, value: string, thumb: Thumb): void;
  85. /**
  86. * - If thumb is `Thumb.START`, removes the attribute on the start input
  87. * (for range slider variant).
  88. * - If thumb is `Thumb.END`, removes the attribute on the end input (or
  89. * only input for single point slider).
  90. */
  91. removeInputAttribute(attribute: string, thumb: Thumb): void;
  92. /**
  93. * - If thumb is `Thumb.START`, focuses start input (range slider variant).
  94. * - If thumb is `Thumb.END`, focuses end input (or only input for single
  95. * point slider).
  96. */
  97. focusInput(thumb: Thumb): void;
  98. /**
  99. * @return Returns true if the given input is focused.
  100. */
  101. isInputFocused(thumb: Thumb): boolean;
  102. /**
  103. * @return Returns true if focus styles should be hidden for pointer events.
  104. */
  105. shouldHideFocusStylesForPointerEvents?(): boolean;
  106. /**
  107. * @return Returns the width of the given thumb knob.
  108. */
  109. getThumbKnobWidth(thumb: Thumb): number;
  110. /**
  111. * @return Returns the bounding client rect of the given thumb.
  112. */
  113. getThumbBoundingClientRect(thumb: Thumb): DOMRect;
  114. /**
  115. * @return Returns the bounding client rect for the slider root element.
  116. */
  117. getBoundingClientRect(): DOMRect;
  118. /**
  119. * @return Returns the width of the given value indicator container.
  120. */
  121. getValueIndicatorContainerWidth(thumb: Thumb): number;
  122. /**
  123. * @return Returns true if the root element is RTL, otherwise false
  124. */
  125. isRTL(): boolean;
  126. /**
  127. * Sets a style property of the thumb element to the passed value.
  128. * - If thumb is `Thumb.START`, sets style on the start thumb (for
  129. * range slider variant).
  130. * - If thumb is `Thumb.END`, sets style on the end thumb (or only thumb
  131. * for single point slider).
  132. */
  133. setThumbStyleProperty(propertyName: string, value: string, thumb: Thumb): void;
  134. /**
  135. * Removes the given style property from the thumb element.
  136. * - If thumb is `Thumb.START`, removes style from the start thumb (for
  137. * range slider variant).
  138. * - If thumb is `Thumb.END`, removes style from the end thumb (or only thumb
  139. * for single point slider).
  140. */
  141. removeThumbStyleProperty(propertyName: string, thumb: Thumb): void;
  142. /**
  143. * Sets a style property of the active track element to the passed value.
  144. */
  145. setTrackActiveStyleProperty(propertyName: string, value: string): void;
  146. /**
  147. * Removes the given style property from the active track element.
  148. */
  149. removeTrackActiveStyleProperty(propertyName: string): void;
  150. /**
  151. * Sets value indicator text based on the given value.
  152. * - If thumb is `Thumb.START`, updates value indicator on start thumb
  153. * (for range slider variant).
  154. * - If thumb is `Thumb.END`, updates value indicator on end thumb (or
  155. * only thumb for single point slider).
  156. */
  157. setValueIndicatorText(value: number, thumb: Thumb): void;
  158. /**
  159. * Returns a function that maps the slider value to the value of the
  160. * `aria-valuetext` attribute on the thumb element. If null, the
  161. * `aria-valuetext` attribute is unchanged when the value changes.
  162. */
  163. getValueToAriaValueTextFn(): ((value: number, thumb: Thumb) => string) | null;
  164. /**
  165. * Updates tick marks container element with tick mark elements and their
  166. * active/inactive classes, based on the given mappings:
  167. * - TickMark.ACTIVE => `cssClasses.TICK_MARK_ACTIVE`
  168. * - TickMark.INACTIVE => `cssClasses.TICK_MARK_INACTIVE`
  169. */
  170. updateTickMarks(tickMarks: TickMark[]): void;
  171. /**
  172. * Sets pointer capture on the slider root.
  173. * https://developer.mozilla.org/en-US/docs/Web/API/Element/setPointerCapture
  174. */
  175. setPointerCapture(pointerId: number): void;
  176. /**
  177. * Emits a `change` event from the slider root, indicating that the value
  178. * has been changed and committed on the given thumb, from a user event.
  179. * Mirrors the native `change` event:
  180. * https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/change_event
  181. */
  182. emitChangeEvent(value: number, thumb: Thumb): void;
  183. /**
  184. * Emits an `input` event from the slider root, indicating that the value
  185. * has been changed on the given thumb, from a user event.
  186. * Mirrors the native `input` event:
  187. * https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event
  188. */
  189. emitInputEvent(value: number, thumb: Thumb): void;
  190. /**
  191. * Emits an event on drag start, containing the current value on the
  192. * thumb being dragged.
  193. */
  194. emitDragStartEvent(value: number, thumb: Thumb): void;
  195. /**
  196. * Emits an event on drag end, containing the final value on the
  197. * thumb that was dragged.
  198. */
  199. emitDragEndEvent(value: number, thumb: Thumb): void;
  200. /**
  201. * Registers an event listener on the root element.
  202. */
  203. registerEventHandler<K extends EventType>(evtType: K, handler: SpecificEventListener<K>): void;
  204. /**
  205. * Deregisters an event listener on the root element.
  206. */
  207. deregisterEventHandler<K extends EventType>(evtType: K, handler: SpecificEventListener<K>): void;
  208. /**
  209. * Registers an event listener on the given thumb element.
  210. */
  211. registerThumbEventHandler<K extends EventType>(thumb: Thumb, evtType: K, handler: SpecificEventListener<K>): void;
  212. /**
  213. * Deregisters an event listener on the given thumb element.
  214. */
  215. deregisterThumbEventHandler<K extends EventType>(thumb: Thumb, evtType: K, handler: SpecificEventListener<K>): void;
  216. /**
  217. * Registers an event listener on the given input element.
  218. */
  219. registerInputEventHandler<K extends EventType>(thumb: Thumb, evtType: K, handler: SpecificEventListener<K>): void;
  220. /**
  221. * Deregisters an event listener on the given input element.
  222. */
  223. deregisterInputEventHandler<K extends EventType>(thumb: Thumb, evtType: K, handler: SpecificEventListener<K>): void;
  224. /**
  225. * Registers an event listener on the body element.
  226. */
  227. registerBodyEventHandler<K extends EventType>(evtType: K, handler: SpecificEventListener<K>): void;
  228. /**
  229. * Deregisters an event listener on the body element.
  230. */
  231. deregisterBodyEventHandler<K extends EventType>(evtType: K, handler: SpecificEventListener<K>): void;
  232. /**
  233. * Registers an event listener on the window.
  234. */
  235. registerWindowEventHandler<K extends EventType>(evtType: K, handler: SpecificEventListener<K>): void;
  236. /**
  237. * Deregisters an event listener on the window.
  238. */
  239. deregisterWindowEventHandler<K extends EventType>(evtType: K, handler: SpecificEventListener<K>): void;
  240. }