Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions resources/js/components/ui/Button/Group.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,33 @@ async function checkOverflow() {
measuringOverflow.value = true;
await nextTick();

// Check if any child has wrapped to a new line
hasOverflow.value = childrenHaveWrapped();

// Exit measuring mode
measuringOverflow.value = false;
}

function childrenHaveWrapped() {
const children = Array.from(group.value.children);
const firstTop = children[0].offsetTop;
const lastTop = children[children.length - 1].offsetTop;
hasOverflow.value = lastTop > firstTop;

// Exit measuring mode
measuringOverflow.value = false;
return lastTop > firstTop;
}

const checkOverflowSoon = debounce(checkOverflow, 50);

// Layout has just been calculated when a ResizeObserver fires, so reading it here is cheap. Only
// measure properly, which forces the browser to recalculate styles, when the layout needs to change.
function resized() {
if (!group.value?.children.length) return;

if (hasOverflow.value || childrenHaveWrapped()) checkOverflowSoon();
}

onMounted(() => {
if (needsOverflowObserver.value) {
checkOverflow();
resizeObserver = new ResizeObserver(debounce(checkOverflow, 50));
resizeObserver = new ResizeObserver(resized);
resizeObserver.observe(wrapper.value);
}
});
Expand Down
66 changes: 66 additions & 0 deletions resources/js/tests/browser/ButtonGroupOverflow.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { afterEach, expect, test } from 'vitest';
import { mount } from '@vue/test-utils';
import { h } from 'vue';
import { Button, ButtonGroup } from '@ui';
import '../../../css/app.css';

let wrapper;
let host;
afterEach(() => {
wrapper?.unmount();
host?.remove();
});

const mountButtonGroup = (width) => {
host = document.createElement('div');
host.style.width = width;
document.body.appendChild(host);

wrapper = mount(
{
render: () =>
h(ButtonGroup, { overflow: 'stack' }, () =>
['Left aligned', 'Centered', 'Right aligned'].map((text) => h(Button, { text })),
),
},
{ attachTo: host },
);

return wrapper.find('[data-ui-button-group]').element;
};

const isStacked = (group) => group.classList.contains('flex-col');

const waitFor = async (condition) => {
for (let i = 0; i < 50 && !condition(); i++) {
await new Promise((resolve) => setTimeout(resolve, 20));
}
};

test('button group stays in a row when it fits', async () => {
const group = mountButtonGroup('800px');

await new Promise((resolve) => setTimeout(resolve, 200));

expect(isStacked(group)).toBe(false);
});

test('button group stacks when it overflows', async () => {
const group = mountButtonGroup('120px');

await waitFor(() => isStacked(group));

expect(isStacked(group)).toBe(true);
});

test('button group stacks and unstacks as it is resized', async () => {
const group = mountButtonGroup('800px');

host.style.width = '120px';
await waitFor(() => isStacked(group));
expect(isStacked(group)).toBe(true);

host.style.width = '800px';
await waitFor(() => !isStacked(group));
expect(isStacked(group)).toBe(false);
});
Loading