|
1 | 1 | <template> |
2 | | - <div |
3 | | - :class="[ |
4 | | - 'group/button inline-flex flex-wrap [[data-floating-toolbar]_&]:justify-center [[data-floating-toolbar]_&]:gap-1 [[data-floating-toolbar]_&]:lg:gap-x-0', |
5 | | - '[&>[data-ui-group-target]:not(:first-child):not(:last-child)]:rounded-none', |
6 | | - '[&>[data-ui-group-target]:first-child:not(:last-child)]:rounded-e-none', |
7 | | - '[&>[data-ui-group-target]:last-child:not(:first-child)]:rounded-s-none', |
8 | | - '[&>*:not(:first-child):not(:last-child):not(:only-child)_[data-ui-group-target]]:rounded-none', |
9 | | - '[&>*:first-child:not(:last-child)_[data-ui-group-target]]:rounded-e-none', |
10 | | - '[&>*:last-child:not(:first-child)_[data-ui-group-target]]:rounded-s-none', |
11 | | - 'dark:[&_button]:ring-0', |
12 | | - 'max-lg:[[data-floating-toolbar]_&_button]:rounded-md!', |
13 | | - 'shadow-ui-sm rounded-lg' |
14 | | - ]" |
15 | | - data-ui-button-group |
16 | | - > |
17 | | - <slot /> |
| 2 | + <div ref="wrapper" :class="{ invisible: measuringOverflow }"> |
| 3 | + <div ref="group" :class="groupClasses" :data-measuring="measuringOverflow || undefined" data-ui-button-group> |
| 4 | + <slot /> |
| 5 | + </div> |
18 | 6 | </div> |
19 | 7 | </template> |
20 | 8 |
|
| 9 | +<script setup> |
| 10 | +import { ref, computed, nextTick, onMounted, onBeforeUnmount } from 'vue'; |
| 11 | +import { cva } from 'cva'; |
| 12 | +
|
| 13 | +import debounce from '@/util/debounce'; |
| 14 | +
|
| 15 | +const props = defineProps({ |
| 16 | + /* When 'stack', switch to vertical layout when overflowing. When 'gap', switch to normal buttons with gaps when overflowing. */ |
| 17 | + overflow: { |
| 18 | + type: String, |
| 19 | + default: null, |
| 20 | + validator: (v) => [null, 'stack', 'gap'].includes(v), |
| 21 | + }, |
| 22 | + orientation: { |
| 23 | + type: String, |
| 24 | + default: 'horizontal', |
| 25 | + }, |
| 26 | + gap: { |
| 27 | + type: [String, Boolean], |
| 28 | + default: false, |
| 29 | + }, |
| 30 | + justify: { |
| 31 | + type: String, |
| 32 | + default: 'start', |
| 33 | + }, |
| 34 | +}); |
| 35 | +
|
| 36 | +const hasOverflow = ref(false); |
| 37 | +const needsOverflowObserver = computed(() => props.overflow === 'stack' || props.overflow === 'gap'); |
| 38 | +const measuringOverflow = ref(false); |
| 39 | +
|
| 40 | +const groupClasses = computed(() => { |
| 41 | + const groupShadow = 'rounded-lg shadow-ui-sm [&_[data-ui-group-target]]:shadow-none'; |
| 42 | +
|
| 43 | + const collapseHorizontally = [ |
| 44 | + '[&>[data-ui-group-target]:not(:first-child):not(:last-child)]:rounded-none', |
| 45 | + '[&>:not(:first-child):not(:last-child)_[data-ui-group-target]]:rounded-none', |
| 46 | + '[&>[data-ui-group-target]:first-child:not(:last-child)]:rounded-e-none', |
| 47 | + '[&>:first-child:not(:last-child)_[data-ui-group-target]]:rounded-e-none', |
| 48 | + '[&>[data-ui-group-target]:last-child:not(:first-child)]:rounded-s-none', |
| 49 | + '[&>:last-child:not(:first-child)_[data-ui-group-target]]:rounded-s-none', |
| 50 | + '[&>[data-ui-group-target]:not(:first-child)]:border-s-0', |
| 51 | + '[&>:not(:first-child)_[data-ui-group-target]]:border-s-0', |
| 52 | + ]; |
| 53 | +
|
| 54 | + const collapseVertically = [ |
| 55 | + 'flex-col', |
| 56 | + '[&>[data-ui-group-target]:not(:first-child):not(:last-child)]:rounded-none', |
| 57 | + '[&>:not(:first-child):not(:last-child)_[data-ui-group-target]]:rounded-none', |
| 58 | + '[&>[data-ui-group-target]:first-child:not(:last-child)]:rounded-b-none', |
| 59 | + '[&>:first-child:not(:last-child)_[data-ui-group-target]]:rounded-b-none', |
| 60 | + '[&>[data-ui-group-target]:last-child:not(:first-child)]:rounded-t-none', |
| 61 | + '[&>:last-child:not(:first-child)_[data-ui-group-target]]:rounded-t-none', |
| 62 | + '[&>[data-ui-group-target]:not(:last-child)]:border-b-0', |
| 63 | + '[&>:not(:last-child)_[data-ui-group-target]]:border-b-0', |
| 64 | + ]; |
| 65 | +
|
| 66 | + return cva({ |
| 67 | + base: [ |
| 68 | + 'group/button inline-flex flex-wrap relative', |
| 69 | + 'dark:[&_button]:ring-0', |
| 70 | + ], |
| 71 | + variants: { |
| 72 | + orientation: { |
| 73 | + vertical: collapseVertically, |
| 74 | + }, |
| 75 | + justify: { |
| 76 | + center: 'justify-center', |
| 77 | + }, |
| 78 | + }, |
| 79 | + compoundVariants: [ |
| 80 | + { overflow: 'stack', hasOverflow: false, class: [...collapseHorizontally, groupShadow] }, |
| 81 | + { overflow: 'stack', hasOverflow: true, class: [...collapseVertically, groupShadow] }, |
| 82 | + { overflow: 'gap', hasOverflow: true, class: 'gap-1' }, |
| 83 | + { overflow: 'gap', hasOverflow: false, class: [...collapseHorizontally, groupShadow] }, |
| 84 | + { overflow: null, orientation: 'horizontal', gap: false, class: [...collapseHorizontally, groupShadow] }, |
| 85 | + ], |
| 86 | + })({ |
| 87 | + gap: props.gap, |
| 88 | + justify: props.justify, |
| 89 | + orientation: props.orientation, |
| 90 | + overflow: props.overflow, |
| 91 | + hasOverflow: hasOverflow.value, |
| 92 | + }); |
| 93 | +}); |
| 94 | +
|
| 95 | +const wrapper = ref(null); |
| 96 | +const group = ref(null); |
| 97 | +let resizeObserver = null; |
| 98 | +
|
| 99 | +async function checkOverflow() { |
| 100 | + if (!group.value?.children.length) return; |
| 101 | +
|
| 102 | + // Measure in collapsed state to avoid hysteresis from gap spacing |
| 103 | + hasOverflow.value = false; |
| 104 | + measuringOverflow.value = true; |
| 105 | + await nextTick(); |
| 106 | +
|
| 107 | + // Check if any child has wrapped to a new line |
| 108 | + const children = Array.from(group.value.children); |
| 109 | + const firstTop = children[0].offsetTop; |
| 110 | + const lastTop = children[children.length - 1].offsetTop; |
| 111 | + hasOverflow.value = lastTop > firstTop; |
| 112 | +
|
| 113 | + // Exit measuring mode |
| 114 | + measuringOverflow.value = false; |
| 115 | +} |
| 116 | +
|
| 117 | +onMounted(() => { |
| 118 | + if (needsOverflowObserver.value) { |
| 119 | + checkOverflow(); |
| 120 | + resizeObserver = new ResizeObserver(debounce(checkOverflow, 50)); |
| 121 | + resizeObserver.observe(wrapper.value); |
| 122 | + } |
| 123 | +}); |
| 124 | +
|
| 125 | +onBeforeUnmount(() => { |
| 126 | + resizeObserver?.disconnect(); |
| 127 | +}); |
| 128 | +</script> |
| 129 | +
|
21 | 130 | <style> |
22 | | - /* GROUP FLOATING TOOLBAR / BUTTON GROUP BORDERS |
23 | | - =================================================== */ |
24 | | - [data-ui-button-group] [data-ui-group-target] { |
25 | | - @apply shadow-none; |
26 | | -
|
27 | | - &:not(:first-child):not([data-floating-toolbar] &) { |
28 | | - border-inline-start: 0; |
29 | | - } |
30 | | -
|
31 | | - /* Account for button groups being split apart on small screens */ |
32 | | - [data-floating-toolbar] & { |
33 | | - @media (width >= 1024px) { |
34 | | - &:not(:first-child) { |
35 | | - border-inline-start: 0; |
36 | | - } |
37 | | - } |
38 | | - } |
| 131 | + /* Force horizontal wrap layout during measurement to detect overflow */ |
| 132 | + [data-ui-button-group][data-measuring] { |
| 133 | + @apply flex! flex-row!; |
39 | 134 | } |
40 | 135 | </style> |
0 commit comments