VSlideGroup.mjs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. import { createVNode as _createVNode } from "vue";
  2. // Styles
  3. import "./VSlideGroup.css";
  4. // Components
  5. import { VFadeTransition } from "../transitions/index.mjs";
  6. import { VIcon } from "../VIcon/index.mjs"; // Composables
  7. import { useDisplay } from "../../composables/index.mjs";
  8. import { makeComponentProps } from "../../composables/component.mjs";
  9. import { makeGroupProps, useGroup } from "../../composables/group.mjs";
  10. import { IconValue } from "../../composables/icons.mjs";
  11. import { useRtl } from "../../composables/locale.mjs";
  12. import { useResizeObserver } from "../../composables/resizeObserver.mjs";
  13. import { makeTagProps } from "../../composables/tag.mjs"; // Utilities
  14. import { computed, shallowRef, watch } from 'vue';
  15. import { bias, calculateCenteredOffset, calculateUpdatedOffset } from "./helpers.mjs";
  16. import { clamp, focusableChildren, genericComponent, IN_BROWSER, propsFactory, useRender } from "../../util/index.mjs"; // Types
  17. export const VSlideGroupSymbol = Symbol.for('vuetify:v-slide-group');
  18. export const makeVSlideGroupProps = propsFactory({
  19. centerActive: Boolean,
  20. direction: {
  21. type: String,
  22. default: 'horizontal'
  23. },
  24. symbol: {
  25. type: null,
  26. default: VSlideGroupSymbol
  27. },
  28. nextIcon: {
  29. type: IconValue,
  30. default: '$next'
  31. },
  32. prevIcon: {
  33. type: IconValue,
  34. default: '$prev'
  35. },
  36. showArrows: {
  37. type: [Boolean, String],
  38. validator: v => typeof v === 'boolean' || ['always', 'desktop', 'mobile'].includes(v)
  39. },
  40. ...makeComponentProps(),
  41. ...makeTagProps(),
  42. ...makeGroupProps({
  43. selectedClass: 'v-slide-group-item--active'
  44. })
  45. }, 'VSlideGroup');
  46. export const VSlideGroup = genericComponent()({
  47. name: 'VSlideGroup',
  48. props: makeVSlideGroupProps(),
  49. emits: {
  50. 'update:modelValue': value => true
  51. },
  52. setup(props, _ref) {
  53. let {
  54. slots
  55. } = _ref;
  56. const {
  57. isRtl
  58. } = useRtl();
  59. const {
  60. mobile
  61. } = useDisplay();
  62. const group = useGroup(props, props.symbol);
  63. const isOverflowing = shallowRef(false);
  64. const scrollOffset = shallowRef(0);
  65. const containerSize = shallowRef(0);
  66. const contentSize = shallowRef(0);
  67. const isHorizontal = computed(() => props.direction === 'horizontal');
  68. const {
  69. resizeRef: containerRef,
  70. contentRect: containerRect
  71. } = useResizeObserver();
  72. const {
  73. resizeRef: contentRef,
  74. contentRect
  75. } = useResizeObserver();
  76. const firstSelectedIndex = computed(() => {
  77. if (!group.selected.value.length) return -1;
  78. return group.items.value.findIndex(item => item.id === group.selected.value[0]);
  79. });
  80. const lastSelectedIndex = computed(() => {
  81. if (!group.selected.value.length) return -1;
  82. return group.items.value.findIndex(item => item.id === group.selected.value[group.selected.value.length - 1]);
  83. });
  84. if (IN_BROWSER) {
  85. let frame = -1;
  86. watch(() => [group.selected.value, containerRect.value, contentRect.value, isHorizontal.value], () => {
  87. cancelAnimationFrame(frame);
  88. frame = requestAnimationFrame(() => {
  89. if (containerRect.value && contentRect.value) {
  90. const sizeProperty = isHorizontal.value ? 'width' : 'height';
  91. containerSize.value = containerRect.value[sizeProperty];
  92. contentSize.value = contentRect.value[sizeProperty];
  93. isOverflowing.value = containerSize.value + 1 < contentSize.value;
  94. }
  95. if (firstSelectedIndex.value >= 0 && contentRef.value) {
  96. // TODO: Is this too naive? Should we store element references in group composable?
  97. const selectedElement = contentRef.value.children[lastSelectedIndex.value];
  98. if (firstSelectedIndex.value === 0 || !isOverflowing.value) {
  99. scrollOffset.value = 0;
  100. } else if (props.centerActive) {
  101. scrollOffset.value = calculateCenteredOffset({
  102. selectedElement,
  103. containerSize: containerSize.value,
  104. contentSize: contentSize.value,
  105. isRtl: isRtl.value,
  106. isHorizontal: isHorizontal.value
  107. });
  108. } else if (isOverflowing.value) {
  109. scrollOffset.value = calculateUpdatedOffset({
  110. selectedElement,
  111. containerSize: containerSize.value,
  112. contentSize: contentSize.value,
  113. isRtl: isRtl.value,
  114. currentScrollOffset: scrollOffset.value,
  115. isHorizontal: isHorizontal.value
  116. });
  117. }
  118. }
  119. });
  120. });
  121. }
  122. const disableTransition = shallowRef(false);
  123. let startTouch = 0;
  124. let startOffset = 0;
  125. function onTouchstart(e) {
  126. const sizeProperty = isHorizontal.value ? 'clientX' : 'clientY';
  127. const sign = isRtl.value && isHorizontal.value ? -1 : 1;
  128. startOffset = sign * scrollOffset.value;
  129. startTouch = e.touches[0][sizeProperty];
  130. disableTransition.value = true;
  131. }
  132. function onTouchmove(e) {
  133. if (!isOverflowing.value) return;
  134. const sizeProperty = isHorizontal.value ? 'clientX' : 'clientY';
  135. const sign = isRtl.value && isHorizontal.value ? -1 : 1;
  136. scrollOffset.value = sign * (startOffset + startTouch - e.touches[0][sizeProperty]);
  137. }
  138. function onTouchend(e) {
  139. const maxScrollOffset = contentSize.value - containerSize.value;
  140. if (scrollOffset.value < 0 || !isOverflowing.value) {
  141. scrollOffset.value = 0;
  142. } else if (scrollOffset.value >= maxScrollOffset) {
  143. scrollOffset.value = maxScrollOffset;
  144. }
  145. disableTransition.value = false;
  146. }
  147. function onScroll() {
  148. if (!containerRef.value) return;
  149. containerRef.value[isHorizontal.value ? 'scrollLeft' : 'scrollTop'] = 0;
  150. }
  151. const isFocused = shallowRef(false);
  152. function onFocusin(e) {
  153. isFocused.value = true;
  154. if (!isOverflowing.value || !contentRef.value) return;
  155. // Focused element is likely to be the root of an item, so a
  156. // breadth-first search will probably find it in the first iteration
  157. for (const el of e.composedPath()) {
  158. for (const item of contentRef.value.children) {
  159. if (item === el) {
  160. scrollOffset.value = calculateUpdatedOffset({
  161. selectedElement: item,
  162. containerSize: containerSize.value,
  163. contentSize: contentSize.value,
  164. isRtl: isRtl.value,
  165. currentScrollOffset: scrollOffset.value,
  166. isHorizontal: isHorizontal.value
  167. });
  168. return;
  169. }
  170. }
  171. }
  172. }
  173. function onFocusout(e) {
  174. isFocused.value = false;
  175. }
  176. function onFocus(e) {
  177. if (!isFocused.value && !(e.relatedTarget && contentRef.value?.contains(e.relatedTarget))) focus();
  178. }
  179. function onKeydown(e) {
  180. if (!contentRef.value) return;
  181. if (isHorizontal.value) {
  182. if (e.key === 'ArrowRight') {
  183. focus(isRtl.value ? 'prev' : 'next');
  184. } else if (e.key === 'ArrowLeft') {
  185. focus(isRtl.value ? 'next' : 'prev');
  186. }
  187. } else {
  188. if (e.key === 'ArrowDown') {
  189. focus('next');
  190. } else if (e.key === 'ArrowUp') {
  191. focus('prev');
  192. }
  193. }
  194. if (e.key === 'Home') {
  195. focus('first');
  196. } else if (e.key === 'End') {
  197. focus('last');
  198. }
  199. }
  200. function focus(location) {
  201. if (!contentRef.value) return;
  202. if (!location) {
  203. const focusable = focusableChildren(contentRef.value);
  204. focusable[0]?.focus();
  205. } else if (location === 'next') {
  206. const el = contentRef.value.querySelector(':focus')?.nextElementSibling;
  207. if (el) el.focus();else focus('first');
  208. } else if (location === 'prev') {
  209. const el = contentRef.value.querySelector(':focus')?.previousElementSibling;
  210. if (el) el.focus();else focus('last');
  211. } else if (location === 'first') {
  212. contentRef.value.firstElementChild?.focus();
  213. } else if (location === 'last') {
  214. contentRef.value.lastElementChild?.focus();
  215. }
  216. }
  217. function scrollTo(location) {
  218. const newAbsoluteOffset = scrollOffset.value + (location === 'prev' ? -1 : 1) * containerSize.value;
  219. scrollOffset.value = clamp(newAbsoluteOffset, 0, contentSize.value - containerSize.value);
  220. }
  221. const contentStyles = computed(() => {
  222. // This adds friction when scrolling the 'wrong' way when at max offset
  223. let scrollAmount = scrollOffset.value > contentSize.value - containerSize.value ? -(contentSize.value - containerSize.value) + bias(contentSize.value - containerSize.value - scrollOffset.value) : -scrollOffset.value;
  224. // This adds friction when scrolling the 'wrong' way when at min offset
  225. if (scrollOffset.value <= 0) {
  226. scrollAmount = bias(-scrollOffset.value);
  227. }
  228. const sign = isRtl.value && isHorizontal.value ? -1 : 1;
  229. return {
  230. transform: `translate${isHorizontal.value ? 'X' : 'Y'}(${sign * scrollAmount}px)`,
  231. transition: disableTransition.value ? 'none' : '',
  232. willChange: disableTransition.value ? 'transform' : ''
  233. };
  234. });
  235. const slotProps = computed(() => ({
  236. next: group.next,
  237. prev: group.prev,
  238. select: group.select,
  239. isSelected: group.isSelected
  240. }));
  241. const hasAffixes = computed(() => {
  242. switch (props.showArrows) {
  243. // Always show arrows on desktop & mobile
  244. case 'always':
  245. return true;
  246. // Always show arrows on desktop
  247. case 'desktop':
  248. return !mobile.value;
  249. // Show arrows on mobile when overflowing.
  250. // This matches the default 2.2 behavior
  251. case true:
  252. return isOverflowing.value || Math.abs(scrollOffset.value) > 0;
  253. // Always show on mobile
  254. case 'mobile':
  255. return mobile.value || isOverflowing.value || Math.abs(scrollOffset.value) > 0;
  256. // https://material.io/components/tabs#scrollable-tabs
  257. // Always show arrows when
  258. // overflowed on desktop
  259. default:
  260. return !mobile.value && (isOverflowing.value || Math.abs(scrollOffset.value) > 0);
  261. }
  262. });
  263. const hasPrev = computed(() => {
  264. return Math.abs(scrollOffset.value) > 0;
  265. });
  266. const hasNext = computed(() => {
  267. // Check one scroll ahead to know the width of right-most item
  268. return contentSize.value > Math.abs(scrollOffset.value) + containerSize.value;
  269. });
  270. useRender(() => _createVNode(props.tag, {
  271. "class": ['v-slide-group', {
  272. 'v-slide-group--vertical': !isHorizontal.value,
  273. 'v-slide-group--has-affixes': hasAffixes.value,
  274. 'v-slide-group--is-overflowing': isOverflowing.value
  275. }, props.class],
  276. "style": props.style,
  277. "tabindex": isFocused.value || group.selected.value.length ? -1 : 0,
  278. "onFocus": onFocus
  279. }, {
  280. default: () => [hasAffixes.value && _createVNode("div", {
  281. "key": "prev",
  282. "class": ['v-slide-group__prev', {
  283. 'v-slide-group__prev--disabled': !hasPrev.value
  284. }],
  285. "onClick": () => scrollTo('prev')
  286. }, [slots.prev?.(slotProps.value) ?? _createVNode(VFadeTransition, null, {
  287. default: () => [_createVNode(VIcon, {
  288. "icon": isRtl.value ? props.nextIcon : props.prevIcon
  289. }, null)]
  290. })]), _createVNode("div", {
  291. "key": "container",
  292. "ref": containerRef,
  293. "class": "v-slide-group__container",
  294. "onScroll": onScroll
  295. }, [_createVNode("div", {
  296. "ref": contentRef,
  297. "class": "v-slide-group__content",
  298. "style": contentStyles.value,
  299. "onTouchstartPassive": onTouchstart,
  300. "onTouchmovePassive": onTouchmove,
  301. "onTouchendPassive": onTouchend,
  302. "onFocusin": onFocusin,
  303. "onFocusout": onFocusout,
  304. "onKeydown": onKeydown
  305. }, [slots.default?.(slotProps.value)])]), hasAffixes.value && _createVNode("div", {
  306. "key": "next",
  307. "class": ['v-slide-group__next', {
  308. 'v-slide-group__next--disabled': !hasNext.value
  309. }],
  310. "onClick": () => scrollTo('next')
  311. }, [slots.next?.(slotProps.value) ?? _createVNode(VFadeTransition, null, {
  312. default: () => [_createVNode(VIcon, {
  313. "icon": isRtl.value ? props.prevIcon : props.nextIcon
  314. }, null)]
  315. })])]
  316. }));
  317. return {
  318. selected: group.selected,
  319. scrollTo,
  320. scrollOffset,
  321. focus
  322. };
  323. }
  324. });
  325. //# sourceMappingURL=VSlideGroup.mjs.map