Live.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import { ARIA_ATOMIC, ARIA_LIVE } from '../../constants/attributes';
  2. import { EVENT_AUTOPLAY_PAUSE, EVENT_AUTOPLAY_PLAY } from '../../constants/events';
  3. import { EventInterface } from '../../constructors';
  4. import { Splide } from '../../core/Splide/Splide';
  5. import { BaseComponent, Components, Options } from '../../types';
  6. import { apply, setAttribute } from '../../utils';
  7. /**
  8. * The interface for the Live component.
  9. *
  10. * @since 3.7.0
  11. */
  12. export interface LiveComponent extends BaseComponent {
  13. disable( disabled: boolean ): void;
  14. }
  15. /**
  16. * The component for implementing Live Region to the slider.
  17. *
  18. * @link https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Live_Regions
  19. *
  20. * @since 3.7.0
  21. *
  22. * @param Splide - A Splide instance.
  23. * @param Components - A collection of components.
  24. * @param options - Options.
  25. *
  26. * @return A Live component object.
  27. */
  28. export function Live( Splide: Splide, Components: Components, options: Options ): LiveComponent {
  29. const { on } = EventInterface( Splide );
  30. const { list } = Components.Elements;
  31. const { live } = options;
  32. /**
  33. * Called when the component is mounted.
  34. * Explicitly sets `aria-atomic` to avoid SR from reading the content twice.
  35. *
  36. * @link https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-atomic
  37. */
  38. function mount(): void {
  39. if ( live ) {
  40. setAttribute( list, ARIA_ATOMIC, false );
  41. disable( ! Components.Autoplay.isPaused() );
  42. on( EVENT_AUTOPLAY_PLAY, apply( disable, true ) );
  43. on( EVENT_AUTOPLAY_PAUSE, apply( disable, false ) );
  44. }
  45. }
  46. /**
  47. * Disables/enables the live region.
  48. * Does nothing when the `live` option is not enabled.
  49. *
  50. * @param disabled - `true` to disable the live region or `false` to enable it again.
  51. */
  52. function disable( disabled: boolean ): void {
  53. if ( live ) {
  54. setAttribute( list, ARIA_LIVE, disabled ? 'off' : 'polite' );
  55. }
  56. }
  57. return {
  58. mount,
  59. disable,
  60. };
  61. }