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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "fix(web-components): resolve auto-resize issues for TextArea in Firefox",
"packageName": "@fluentui/web-components",
"email": "198982749+Copilot@users.noreply.github.com",
"dependentChangeType": "patch"
}
2 changes: 2 additions & 0 deletions packages/web-components/docs/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,8 @@ export class BaseTextArea extends FASTElement {
// (undocumented)
protected readOnlyChanged(): void;
reportValidity(): boolean;
// @internal
rootEl: HTMLDivElement;
required: boolean;
// (undocumented)
protected requiredChanged(): void;
Expand Down
2 changes: 2 additions & 0 deletions packages/web-components/docs/web-components.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,8 @@ export class BaseTextArea extends FASTElement {
resize: TextAreaResize;
// (undocumented)
protected resizeChanged(prev: TextAreaResize | undefined, next: TextAreaResize | undefined): void;
// @internal
rootEl: HTMLDivElement;
select(): void;
setCustomValidity(message: string | null): void;
// @internal
Expand Down
11 changes: 10 additions & 1 deletion packages/web-components/src/textarea/textarea.base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ export class BaseTextArea extends FASTElement {
*/
public labelEl!: HTMLLabelElement;

/**
* The root container element.
* @internal
*/
public rootEl!: HTMLDivElement;

/**
* The `<textarea>` element.
* @internal
Expand Down Expand Up @@ -587,7 +593,10 @@ export class BaseTextArea extends FASTElement {
this.autoSizerEl.classList.add('auto-sizer');
this.autoSizerEl.ariaHidden = 'true';
}
this.shadowRoot!.prepend(this.autoSizerEl);
// `rootEl` uses optional chaining because `autoResizeChanged` may be called before
// the element connects and the template renders. `connectedCallback` will call this
// method again once `rootEl` is available.
this.rootEl?.prepend(this.autoSizerEl);

// The `ResizeObserver` is used to observe when the component gains
// explicit block size, when so, the `autoSizerEl` element should be
Expand Down
40 changes: 40 additions & 0 deletions packages/web-components/src/textarea/textarea.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,46 @@ test.describe('TextArea', () => {
await expect(element).not.toHaveAttribute('auto-resize');
});

test('should place the auto-sizer element inside the root element when `field-sizing: content` is not supported', async ({
fastPage,
page,
}) => {
const { element } = fastPage;

// Mock CSS.supports to simulate a browser that doesn't support `field-sizing: content` (e.g. Firefox)
// and restore it after to avoid affecting subsequent tests
await page.evaluate(() => {
const originalSupports = CSS.supports.bind(CSS);
(window as Window & { __originalCSSSupports?: typeof CSS.supports }).__originalCSSSupports = originalSupports;
CSS.supports = (property: string, value?: string) => {
if (property === 'field-sizing: content' || (property === 'field-sizing' && value === 'content')) {
return false;
}
return originalSupports(property, value as string);
};
});

try {
await fastPage.setTemplate({ attributes: { 'auto-resize': true } });

const autoSizerIsInsideRoot = await element.evaluate((node: TextArea) => {
const root = node.shadowRoot?.querySelector('.root');
return root?.contains(node.autoSizerEl ?? null) ?? false;
});

expect(autoSizerIsInsideRoot).toBe(true);
} finally {
// Restore the original CSS.supports to avoid affecting other tests
await page.evaluate(() => {
const w = window as Window & { __originalCSSSupports?: typeof CSS.supports };
if (w.__originalCSSSupports) {
CSS.supports = w.__originalCSSSupports;
delete w.__originalCSSSupports;
}
});
}
});

test('should toggle block attribute', async ({ fastPage }) => {
const { element } = fastPage;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export function textAreaTemplate<T extends TextArea>(): ElementViewTemplate<T> {
<label ${ref('labelEl')} for="control" part="label">
<slot name="label" ${slotted('labelSlottedNodes')}></slot>
</label>
<div class="root" part="root">
<div class="root" part="root" ${ref('rootEl')}>
<textarea
${ref('controlEl')}
id="control"
Expand Down