Keyboard.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import { ARROW_LEFT, ARROW_RIGHT } from '../../constants/arrows';
  2. import { EVENT_MOVE, EVENT_UPDATED } from '../../constants/events';
  3. import { EventInterface } from '../../constructors';
  4. import { Splide } from '../../core/Splide/Splide';
  5. import { BaseComponent, Components, Options } from '../../types';
  6. import { nextTick } from '../../utils';
  7. import { normalizeKey } from '../../utils/dom/normalizeKey/normalizeKey';
  8. /**
  9. * The interface for the Keyboard component.
  10. *
  11. * @since 3.0.0
  12. */
  13. export interface KeyboardComponent extends BaseComponent {
  14. disable( disabled: boolean ): void;
  15. }
  16. /**
  17. * The keyboard event name.
  18. *
  19. * @since 3.6.0
  20. */
  21. const KEYBOARD_EVENT = 'keydown';
  22. /**
  23. * The component for controlling the slider by keyboards.
  24. *
  25. * @since 3.0.0
  26. *
  27. * @param Splide - A Splide instance.
  28. * @param Components - A collection of components.
  29. * @param options - Options.
  30. *
  31. * @return A Keyboard component object.
  32. */
  33. export function Keyboard( Splide: Splide, Components: Components, options: Options ): KeyboardComponent {
  34. const { on, bind, unbind } = EventInterface( Splide );
  35. const { root } = Splide;
  36. const { resolve } = Components.Direction;
  37. /**
  38. * The target element of the keyboard event.
  39. */
  40. let target: Window | HTMLElement;
  41. /**
  42. * Indicates whether the component is currently disabled or not.
  43. */
  44. let disabled: boolean;
  45. /**
  46. * Called when the component is mounted.
  47. */
  48. function mount(): void {
  49. init();
  50. on( EVENT_UPDATED, destroy );
  51. on( EVENT_UPDATED, init );
  52. on( EVENT_MOVE, onMove );
  53. }
  54. /**
  55. * Initializes the component.
  56. */
  57. function init(): void {
  58. const { keyboard } = options;
  59. if ( keyboard ) {
  60. target = keyboard === 'global' ? window : root;
  61. bind( target, KEYBOARD_EVENT, onKeydown );
  62. }
  63. }
  64. /**
  65. * Destroys the component.
  66. */
  67. function destroy(): void {
  68. unbind( target, KEYBOARD_EVENT );
  69. }
  70. /**
  71. * Disables the keyboard input.
  72. *
  73. * @param value - Toggles disabling/enabling the keyboard input.
  74. */
  75. function disable( value: boolean ): void {
  76. disabled = value;
  77. }
  78. /**
  79. * Called when the slider moves.
  80. * To avoid the slider from moving twice, wait for a tick.
  81. */
  82. function onMove(): void {
  83. const _disabled = disabled;
  84. disabled = true;
  85. nextTick( () => { disabled = _disabled } );
  86. }
  87. /**
  88. * Called when any key is pressed on the target.
  89. *
  90. * @param e - A KeyboardEvent object.
  91. */
  92. function onKeydown( e: KeyboardEvent ): void {
  93. if ( ! disabled ) {
  94. const key = normalizeKey( e );
  95. if ( key === resolve( ARROW_LEFT ) ) {
  96. Splide.go( '<' );
  97. } else if ( key === resolve( ARROW_RIGHT ) ) {
  98. Splide.go( '>' );
  99. }
  100. }
  101. }
  102. return {
  103. mount,
  104. destroy,
  105. disable,
  106. };
  107. }