Keyboard.ts 2.7 KB

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