Direction.ts 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { ARROW_DOWN, ARROW_LEFT, ARROW_RIGHT, ARROW_UP } from '../../constants/arrows';
  2. import { RTL, TTB } from '../../constants/directions';
  3. import { Splide } from '../../core/Splide/Splide';
  4. import { BaseComponent, ComponentConstructor, Components, Options } from '../../types';
  5. import { apply } from '../../../../../utils';
  6. /**
  7. * The interface for the Direction component.
  8. *
  9. * @since 3.0.0
  10. */
  11. export interface DirectionComponent extends BaseComponent {
  12. resolve<K extends keyof typeof ORIENTATION_MAP>(prop: K, axisOnly?: boolean, direction?: Options['direction']): typeof ORIENTATION_MAP[ K ][ number ] | K;
  13. resolve<R extends string>(prop: R, axisOnly?: boolean, direction?: Options['direction']): R;
  14. orient(value: number): number;
  15. left(): string;
  16. right(): string;
  17. width(): string;
  18. }
  19. /**
  20. * The translation map for directions.
  21. *
  22. * @since 3.0.0
  23. */
  24. export const ORIENTATION_MAP = {
  25. width: ['height'],
  26. left: ['top', 'right'],
  27. right: ['bottom', 'left'],
  28. x: ['y'],
  29. X: ['Y'],
  30. Y: ['X'],
  31. ArrowLeft: [ARROW_UP, ARROW_RIGHT],
  32. ArrowRight: [ARROW_DOWN, ARROW_LEFT],
  33. } as const;
  34. /**
  35. * The component that absorbs the difference among directions.
  36. *
  37. * @since 3.0.0
  38. *
  39. * @param Splide - A Splide instance.
  40. * @param Components - A collection of components.
  41. * @param options - Options.
  42. *
  43. * @return A Direction component object.
  44. */
  45. export const Direction: ComponentConstructor<DirectionComponent> = (Splide: Splide, Components: Components, options: Options) => {
  46. /**
  47. * Resolves the provided property name.
  48. *
  49. * @param prop - A property name to translate.
  50. * @param axisOnly - Optional. If `ture`, returns the same property for LTR and RTL.
  51. * @param direction - Optional. Specify the direction. The default value is the `direction` option.
  52. */
  53. function resolve(
  54. prop: string,
  55. axisOnly?: boolean,
  56. direction: Options[ 'direction' ] = options.direction,
  57. ): string {
  58. const index = direction === RTL && !axisOnly ? 1 : direction === TTB ? 0 : -1;
  59. return ORIENTATION_MAP[prop] && ORIENTATION_MAP[prop][index]
  60. || prop.replace(/width|left|right/i, (match, offset) => {
  61. const replacement = ORIENTATION_MAP[match.toLowerCase()][index] || match;
  62. return offset > 0 ? replacement.charAt(0).toUpperCase() + replacement.slice(1) : replacement;
  63. });
  64. }
  65. /**
  66. * Orients the value towards the current direction.
  67. *
  68. * @param value - A value to orient.
  69. * @param direction - Optional. Specify the direction. The default value is the `direction` option.
  70. *
  71. * @return The oriented value.
  72. */
  73. function orient(value: number, direction: Options[ 'direction' ] = options.direction): number {
  74. return value * (direction === RTL ? 1 : -1);
  75. }
  76. return {
  77. resolve,
  78. orient,
  79. left: apply(resolve, 'left'),
  80. right: apply(resolve, 'right'),
  81. width: apply(resolve, 'width'),
  82. };
  83. };