mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-29 19:36:20 -05:00
ui: a row of buttons is spaced by the row
The archive page's six actions shared edges, and Update sat lower than the rest. Both come from the same thing: Semantic's Button carried `margin: 0 .25em .25em 0`, so a page could list buttons as bare siblings and they arrived spaced -- and separated from the line below when the row wrapped. Nothing replaced that margin. Two of those six answered with `marginTop: 0.5em`, which is the wrong axis: a Button is `display: inline-flex; vertical-align: middle`, and `vertical-align` is measured on the MARGIN box, so half of any top margin shows up as the visible button dropping. It was covering the wrapped-row case in the only axis a margin can, and paying for it in the other one. `.wrolpi-button-row` is a flex row with a gap, so both axes are covered at once and no button needs a margin of its own -- the same rule as `.wrolpi-stack`, along the other axis. Applied to the five rows in the app that had no gap: the archive page, the shared collection edit form (which three pages hand action buttons to, and where nine buttons plus Cancel carried `margin-top: 1em` each while Save carried none), the doc page's two responsive branches, and the downloads table's edit/restart pair. `.action-button-spacing` goes with them; it was that same margin, on Cancel. Guarded four ways. ui-layout.cy.js measures that the row really spaces what it holds, wraps rather than overflows, centres its buttons against a taller item beside them, and counts a confirmed APIButton as one item rather than two -- that one returns a fragment holding a `<Confirm/>`, which would open a hole in the row if it did not portal. Common.test.js scans for the two ways this comes back: a button in a row stamping a vertical margin, and a row of buttons that is not a row. Neither throws, which is how the app got here. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
bb14bf4074
commit
5ce4f2ebf3
12 changed files with 464 additions and 44 deletions
|
|
@ -277,6 +277,136 @@ describe('a page stacks its own blocks', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('a row of buttons is spaced and aligned by the row', () => {
|
||||
/*
|
||||
* Semantic's Button carried `margin: 0 .25em .25em 0` of its own, so a page could list
|
||||
* buttons as bare siblings and they arrived spaced. Nothing replaced that margin, and
|
||||
* five pages still list them that way -- the archive page's six actions all shared edges.
|
||||
*
|
||||
* Two of those six also carried `marginTop: 0.5em`, which is what made "Update" sit lower
|
||||
* than its neighbours: a Button is `display: inline-flex; vertical-align: middle`, and
|
||||
* `vertical-align` aligns the MARGIN box, so half of any top margin pushes the visible
|
||||
* button down. The margin was there to keep a wrapped row from touching vertically --
|
||||
* the right thing, solved in the wrong axis. A flex row with `gap` covers both axes and
|
||||
* needs no per-button margin, so the alignment comes back for free.
|
||||
*/
|
||||
|
||||
const rowButtons = ($row) => [...$row[0].querySelectorAll('button')];
|
||||
|
||||
it('puts a gap between the buttons, horizontally and when wrapped', () => {
|
||||
cy.mountUI(<div className='wrolpi-button-row'>
|
||||
<Button>View</Button>
|
||||
<Button>Read</Button>
|
||||
<Button>Update</Button>
|
||||
</div>);
|
||||
|
||||
cy.get('.wrolpi-button-row').should(($row) => {
|
||||
const row = $row[0];
|
||||
const styles = getComputedStyle(row);
|
||||
const declared = parseFloat(styles.columnGap);
|
||||
expect(declared, 'the row declares a gap').to.be.greaterThan(0);
|
||||
expect(parseFloat(styles.rowGap), 'and the same one between wrapped lines')
|
||||
.to.be.closeTo(declared, 0.5);
|
||||
expect(styles.flexWrap, 'the row wraps rather than overflowing').to.equal('wrap');
|
||||
|
||||
/*
|
||||
* Narrowed to exactly two buttons' worth, measured rather than guessed. A hardcoded
|
||||
* 150px was a hidden assertion about both the font and the interface scale, and at 1.1
|
||||
* it fitted one button, not the two the assertions below are about.
|
||||
*/
|
||||
const widths = rowButtons($row).map(el => el.getBoundingClientRect().width);
|
||||
expect(widths, 'three buttons measured').to.have.length(3);
|
||||
row.style.width = `${widths[0] + declared + widths[1] + 1}px`;
|
||||
|
||||
const rects = rowButtons($row).map(el => el.getBoundingClientRect());
|
||||
const top = Math.min(...rects.map(rect => rect.top));
|
||||
const first = rects.filter(rect => Math.round(rect.top) === Math.round(top));
|
||||
expect(first, 'two buttons on the first line').to.have.length(2);
|
||||
expect(first[1].left - first[0].right, 'gap along the line').to.be.closeTo(declared, 0.5);
|
||||
|
||||
// And the third dropped to a second line, held off the first by the same gap.
|
||||
const wrapped = rects.find(rect => Math.round(rect.top) !== Math.round(top));
|
||||
expect(wrapped, 'the third button wrapped').to.not.be.undefined;
|
||||
expect(wrapped.top - first[0].bottom, 'gap between the lines')
|
||||
.to.be.closeTo(declared, 0.5);
|
||||
});
|
||||
});
|
||||
|
||||
it('centres its buttons against a taller item beside them', () => {
|
||||
/*
|
||||
* A row is not always all buttons: the doc page puts its format picker beside its actions,
|
||||
* and the downloads table an error trigger beside its two icon buttons. Whatever is
|
||||
* tallest sets the line height, and `align-items: center` is what keeps the buttons reading
|
||||
* as part of that line rather than hanging from the top of it.
|
||||
*
|
||||
* Written as tops first, which was a test with no teeth -- `stretch` stretches the WRAPPER
|
||||
* and leaves the button at the top of it, so the tops agreed either way. Centres are what
|
||||
* the property actually decides.
|
||||
*
|
||||
* The row is also mixed in the way the archive page's is: a button inside an `<a>`, a bare
|
||||
* one, and an APIButton.
|
||||
*/
|
||||
cy.mountUI(<div className='wrolpi-button-row'>
|
||||
<div style={{height: '80px', width: '40px'}}>Tall</div>
|
||||
<a href='/media/archive.html'><Button>View</Button></a>
|
||||
<Button>Read</Button>
|
||||
<APIButton onClick={() => Promise.resolve()}>Update</APIButton>
|
||||
</div>);
|
||||
|
||||
cy.get('.wrolpi-button-row').should(($row) => {
|
||||
const row = $row[0].getBoundingClientRect();
|
||||
expect(row.height, 'the tall item set the line height').to.be.at.least(80);
|
||||
|
||||
const centers = rowButtons($row).map((el) => {
|
||||
const rect = el.getBoundingClientRect();
|
||||
return rect.top + rect.height / 2;
|
||||
});
|
||||
expect(centers, 'three buttons measured').to.have.length(3);
|
||||
const middle = row.top + row.height / 2;
|
||||
centers.forEach(center => expect(center, 'centred in the line').to.be.closeTo(middle, 1));
|
||||
});
|
||||
});
|
||||
|
||||
it('counts a confirmed action as one item in the row, not two', () => {
|
||||
/*
|
||||
* `APIButton` with `confirmContent` returns a fragment -- the button AND a `<Confirm/>`.
|
||||
* In a flex row those flatten into siblings, so a Confirm that rendered anything in
|
||||
* place would take a gap of its own and open a hole between two buttons. It portals,
|
||||
* and this is what says so; three of the archive page's six actions are confirmed.
|
||||
*/
|
||||
cy.mountUI(<div className='wrolpi-button-row'>
|
||||
<Button>Plain</Button>
|
||||
<APIButton confirmContent='Sure?' onClick={() => Promise.resolve()}>Delete</APIButton>
|
||||
<Button>Also plain</Button>
|
||||
</div>);
|
||||
|
||||
cy.get('.wrolpi-button-row').should(($row) => {
|
||||
const declared = parseFloat(getComputedStyle($row[0]).columnGap);
|
||||
const rects = rowButtons($row).map(el => el.getBoundingClientRect());
|
||||
expect(rects, 'three buttons measured').to.have.length(3);
|
||||
rects.slice(1).forEach((rect, index) => expect(
|
||||
rect.left - rects[index].right, 'gap either side of the confirmed action',
|
||||
).to.be.closeTo(declared, 0.5));
|
||||
});
|
||||
});
|
||||
|
||||
it('grows its gap with the interface scale', () => {
|
||||
cy.mountUI(<div className='wrolpi-button-row'><Button>One</Button><Button>Two</Button></div>);
|
||||
|
||||
cy.get('.wrolpi-button-row').should(($row) => {
|
||||
const before = parseFloat(getComputedStyle($row[0]).columnGap);
|
||||
const root = Cypress.$('html')[0];
|
||||
const scale = parseFloat(getComputedStyle(root).getPropertyValue('--ui-scale')) || 1;
|
||||
root.style.setProperty('--ui-scale', String(scale * 2));
|
||||
const after = parseFloat(getComputedStyle($row[0]).columnGap);
|
||||
root.style.removeProperty('--ui-scale');
|
||||
|
||||
expect(before, 'the gap exists at all').to.be.greaterThan(0);
|
||||
expect(after / before, 'and doubles with the scale').to.be.closeTo(2, 0.05);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
/* `--panel` is authored as a hex; computed backgrounds come back as rgb(). */
|
||||
const hexToRgb = (hex) => {
|
||||
const value = hex.replace('#', '');
|
||||
|
|
|
|||
|
|
@ -1306,6 +1306,36 @@ html[data-theme="amber"] .wrolpi-progress-fill {
|
|||
gap: 1rem;
|
||||
}
|
||||
|
||||
/*
|
||||
* The same idea along the other axis: a row of buttons, spaced by the row.
|
||||
*
|
||||
* Semantic's Button carried `margin: 0 .25em .25em 0`, so a page could list buttons as bare
|
||||
* siblings and they arrived both spaced and, when the row ran out of width, separated from the line
|
||||
* below. Nothing replaced that margin -- the archive page's six actions shared edges, and the
|
||||
* downloads table's two icon buttons did too.
|
||||
*
|
||||
* Two of the archive page's six had `marginTop: 0.5em` instead, which is where the "Update sits
|
||||
* lower than the rest" came from: a Button is `display: inline-flex; vertical-align: middle`, and
|
||||
* `vertical-align` aligns the MARGIN box, so half of any top margin shows up as the visible button
|
||||
* dropping. The margin was solving the wrapped-row case in the only axis a margin can, and paying
|
||||
* for it in the other one. A gap covers both at once, so no button needs a margin of its own; the
|
||||
* scans in Common.test.js are what keep them from growing back.
|
||||
*
|
||||
* 0.625rem matches `.wrolpi-modal-actions`, which is the same row in a modal footer.
|
||||
*/
|
||||
.wrolpi-button-row {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
/*
|
||||
* Centred, not stretched. Rows here are not all buttons -- the doc page puts its format
|
||||
* picker beside its actions, and the downloads table its error trigger -- and a taller item
|
||||
* makes the line taller than a button. `stretch` would leave the buttons hanging from the
|
||||
* top of it; centring keeps a row of unequal things reading as one line.
|
||||
*/
|
||||
align-items: center;
|
||||
gap: 0.625rem;
|
||||
}
|
||||
|
||||
/* ----------------------------------------------------------------- Panels */
|
||||
|
||||
.wrolpi-panel {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue