Skip to content

Commit

Permalink
feat: ensure fields always stay within column boundaries (#8762)
Browse files Browse the repository at this point in the history
  • Loading branch information
vursen authored Mar 5, 2025
1 parent 600cd3e commit d05f821
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/form-layout/src/vaadin-form-layout-mixin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import type { Constructor } from '@open-wc/dedupe-mixin';
import type { ResizeMixinClass } from '@vaadin/component-base/src/resize-mixin.js';
import type { SlotStylesMixinClass } from '@vaadin/component-base/src/slot-styles-mixin.js';

export type FormLayoutLabelsPosition = 'aside' | 'top';

Expand All @@ -19,7 +20,7 @@ export type FormLayoutResponsiveStep = {
*/
export declare function FormLayoutMixin<T extends Constructor<HTMLElement>>(
base: T,
): Constructor<ResizeMixinClass> & Constructor<FormLayoutMixinClass> & T;
): Constructor<ResizeMixinClass> & Constructor<FormLayoutMixinClass> & Constructor<SlotStylesMixinClass> & T;

export declare class FormLayoutMixinClass {
/**
Expand Down
12 changes: 11 additions & 1 deletion packages/form-layout/src/vaadin-form-layout-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
import { isElementHidden } from '@vaadin/a11y-base/src/focus-utils.js';
import { ResizeMixin } from '@vaadin/component-base/src/resize-mixin.js';
import { SlotStylesMixin } from '@vaadin/component-base/src/slot-styles-mixin.js';
import { formLayoutSlotStyles } from './vaadin-form-layout-styles.js';

function isValidCSSLength(value) {
// Check if the value is a valid CSS length and not `inherit` or `normal`,
Expand All @@ -27,7 +29,7 @@ function isBreakLine(el) {
* @mixes ResizeMixin
*/
export const FormLayoutMixin = (superClass) =>
class extends ResizeMixin(superClass) {
class extends SlotStylesMixin(ResizeMixin(superClass)) {
static get properties() {
return {
/**
Expand Down Expand Up @@ -250,6 +252,14 @@ export const FormLayoutMixin = (superClass) =>
this.__childrenAttributesObserver.disconnect();
}

/**
* @override
* @protected
*/
get slotStyles() {
return [`${formLayoutSlotStyles}`.replace('vaadin-form-layout', this.localName)];
}

/** @private */
_naturalNumberOrOne(n) {
if (typeof n === 'number' && n >= 1 && n < Infinity) {
Expand Down
12 changes: 12 additions & 0 deletions packages/form-layout/src/vaadin-form-layout-styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export const formLayoutStyles = css`
/* By default, place each child on a new row */
grid-column: 1 / span min(var(--_grid-colspan, 1), var(--_grid-rendered-column-count));
box-sizing: border-box;
max-width: 100%;
}
:host([auto-responsive][auto-rows]) #layout ::slotted(*) {
Expand Down Expand Up @@ -146,6 +148,14 @@ export const formLayoutStyles = css`
}
`;

export const formLayoutSlotStyles = css`
/* Using :where to ensure user styles always take precedence */
:where(vaadin-form-layout[auto-responsive] vaadin-form-item > *) {
box-sizing: border-box;
max-width: 100%;
}
`;

export const formRowStyles = css`
:host {
display: contents;
Expand All @@ -161,6 +171,8 @@ export const formRowStyles = css`
--_form-item-labels-aside: inherit;
grid-column: auto / span min(var(--_grid-colspan, 1), var(--_grid-rendered-column-count));
box-sizing: border-box;
max-width: 100%;
}
::slotted(:first-child) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,35 @@ describe('form-layout auto responsive', () => {
await visualDiff(container, 'colspan-with-explicit-rows');
});
});

describe('fields with explicit width', () => {
beforeEach(async () => {
element = fixtureSync(
`
<vaadin-form-layout auto-responsive column-width="15em">
<vaadin-form-row>
<input placeholder="First name" style="width: 10em" />
<input placeholder="Last Name" style="width: 20em" />
</vaadin-form-row>
<vaadin-form-row>
<vaadin-form-item>
<label slot="label">Phone</label>
<input style="width: 10em" />
</vaadin-form-item>
<vaadin-form-item>
<label slot="label">Email</label>
<input style="width: 20em" />
</vaadin-form-item>
</vaadin-form-row>
</vaadin-form-layout>
`,
container,
);
await nextFrame();
});

it('default', async () => {
await visualDiff(container, 'fields-with-explicit-width');
});
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -354,4 +354,35 @@ describe('form-layout auto responsive', () => {
await visualDiff(container, 'colspan-with-explicit-rows');
});
});

describe('fields with explicit width', () => {
beforeEach(async () => {
element = fixtureSync(
`
<vaadin-form-layout auto-responsive column-width="15em">
<vaadin-form-row>
<input placeholder="First name" style="width: 10em" />
<input placeholder="Last Name" style="width: 20em" />
</vaadin-form-row>
<vaadin-form-row>
<vaadin-form-item>
<label slot="label">Phone</label>
<input style="width: 10em" />
</vaadin-form-item>
<vaadin-form-item>
<label slot="label">Email</label>
<input style="width: 20em" />
</vaadin-form-item>
</vaadin-form-row>
</vaadin-form-layout>
`,
container,
);
await nextFrame();
});

it('default', async () => {
await visualDiff(container, 'fields-with-explicit-width');
});
});
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d05f821

Please sign in to comment.