Skip to content
Open
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ All notable changes for each version of this project will be documented in this
- In Safari, the horizontal scrollbar of a horizontally virtualized `igxFor` is now visible and usable when the list first renders. Previously it was collapsed until change detection ran again.
- `IgxGridComponent`, `IgxTreeGridComponent`, `IgxHierarchicalGridComponent`
- Auto-sizing columns with `width="auto"` no longer throws when a column group is partly scrolled out of view and one of its child columns with a cell template has no rendered cells. Such a column is now measured when it is scrolled into view.
- `IgxGridComponent`, `IgxTreeGridComponent`, `IgxHierarchicalGridComponent`, `IgxPivotGridComponent`
- A grid in a shadow root, such as the shadow root of a component with `ViewEncapsulation.ShadowDom` or of a custom element, now gets its structural styles. The grid adds its stylesheet to that shadow root as well as to the document. Previously only the document had the stylesheet, so the grid had no layout in a shadow root (#17859).
- In a shadow root, vertical scrolling and the filter row now work. Previously, in a grid that renders more than 100 cells, each scroll threw `Cannot read properties of null (reading 'scrollTop')` and the rows did not follow the scrollbar. Text typed in the filter row threw an error and did not filter the grid (#17860).
- `IgxStylesRegistrar`
- `register()` takes an optional `host` element. When the host is in a shadow root, the stylesheet is added to that shadow root as well as to the document (#17859).
- `provideIgniteIntl()`
- The US time zone abbreviations `UT`, `EDT`, `CST`, `CDT`, `MDT`, `PST` and `PDT`, in any letter case, are formatted with fixed offsets, as with the Angular date pipe. This applies to date formatting such as `IColumnPipeArgs.timezone` and the date pipe. A format that shows the zone shows the offset, such as `GMT-8` for `PST`. Previously `UT`, `EDT`, `CDT`, `MDT` and `PDT` produced the unformatted date text, and `CST` and `PST` were shifted by daylight saving time. `EST` and `MST` are formatted as before (#18031).
- `IgxPivotGridComponent`
Expand Down
25 changes: 25 additions & 0 deletions projects/igniteui-angular-elements/src/app/custom-strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,31 @@ describe('Elements: ', () => {
gridEl.selectRows([1, 2]);
expect((await selectionChanged).detail).toEqual([1, 2]);
});

describe('in a shadow root', () => {
let root: ShadowRoot;

beforeEach(() => {
root = testContainer.attachShadow({ mode: 'open' });
});

it('should adopt the structural styles of the grid into the shadow root', async () => {
const gridEl = document.createElement('igc-grid');
gridEl.autoGenerate = true;
gridEl.data = SampleTestData.foodProductData();
root.appendChild(gridEl);

let cell: HTMLElement | null = null;
for (let waited = 0; waited < 3000 && !cell; waited += 20) {
await firstValueFrom(timer(20));
cell = gridEl.querySelector('igx-grid-cell');
}

// The structural styles of the grid are an adopted stylesheet, which the document shares with the shadow root.
expect(root.adoptedStyleSheets.some(sheet => document.adoptedStyleSheets.includes(sheet))).toBe(true);
expect(getComputedStyle(cell!).display).toBe('flex');
});
});
});

describe('Element property handling.', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,106 @@ describe('IgxStylesRegistrar', () => {
expect(style).toBeDefined();
expect(document.adoptedStyleSheets.length).toBe(initialSheets.length);
});

describe('with a host', () => {
let container: HTMLElement;

beforeEach(() => {
container = document.createElement('div');
document.body.appendChild(container);
});

afterEach(() => {
container.remove();
});

const hostInShadowRoot = () => {
const root = container.attachShadow({ mode: 'open' });
const host = document.createElement('div');
root.appendChild(host);
return { root, host };
};

it('should adopt the stylesheet into the shadow root of the host and into the document', () => {
const { root, host } = hostInShadowRoot();

TestBed.inject(IgxStylesRegistrar).register(Symbol('shadow'), '.igx-register-shadow { color: red; }', host);

const added = document.adoptedStyleSheets.slice(initialSheets.length);
expect(added.length).toBe(1);
expect(root.adoptedStyleSheets).toEqual([added[0]]);
});

it('should adopt the stylesheet into a shadow root once per id', () => {
const { root, host } = hostInShadowRoot();
const sibling = document.createElement('div');
root.appendChild(sibling);
const registrar = TestBed.inject(IgxStylesRegistrar);
const id = Symbol('shadow-once');

registrar.register(id, '.igx-register-shadow-once { color: red; }', host);
registrar.register(id, '.igx-register-shadow-once { color: red; }', sibling);

expect(root.adoptedStyleSheets.length).toBe(1);
});

it('should adopt an already registered stylesheet into a new shadow root', () => {
const registrar = TestBed.inject(IgxStylesRegistrar);
const id = Symbol('shadow-later');
registrar.register(id, '.igx-register-shadow-later { color: red; }');
const { root, host } = hostInShadowRoot();

registrar.register(id, '.igx-register-shadow-later { color: red; }', host);

const added = document.adoptedStyleSheets.slice(initialSheets.length);
expect(added.length).toBe(1);
expect(root.adoptedStyleSheets).toEqual([added[0]]);
});

it('should keep the stylesheets that the shadow root already has', () => {
const { root, host } = hostInShadowRoot();
const own = new CSSStyleSheet();
root.adoptedStyleSheets = [own];

TestBed.inject(IgxStylesRegistrar).register(Symbol('shadow-keep'), '.igx-register-shadow-keep { color: red; }', host);

expect(root.adoptedStyleSheets.length).toBe(2);
expect(root.adoptedStyleSheets[0]).toBe(own);
});

it('should adopt the stylesheet into the document only for a host in the document', () => {
const host = document.createElement('div');
container.appendChild(host);

TestBed.inject(IgxStylesRegistrar).register(Symbol('light'), '.igx-register-light { color: red; }', host);

expect(document.adoptedStyleSheets.length - initialSheets.length).toBe(1);
});

it('should adopt the stylesheet into the document only for a host that is not connected', () => {
const host = document.createElement('div');

TestBed.inject(IgxStylesRegistrar).register(Symbol('detached'), '.igx-register-detached { color: red; }', host);

expect(document.adoptedStyleSheets.length - initialSheets.length).toBe(1);
});

it('should append a style element to the document only outside the browser', () => {
TestBed.configureTestingModule({
providers: [{ provide: PlatformUtil, useValue: { isBrowser: false } }]
});
const { root, host } = hostInShadowRoot();

TestBed.inject(IgxStylesRegistrar).register(Symbol('server-shadow'), '.igx-register-server-shadow { color: red; }', host);

const style = Array.from(document.head.querySelectorAll('style'))
.find(s => s.textContent?.includes('.igx-register-server-shadow'));
style?.setAttribute('data-registrar-test', '');
expect(style).toBeDefined();
expect(root.adoptedStyleSheets.length).toBe(0);
expect(root.querySelector('style')).toBeNull();
});
});
});

describe('retain', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ import { PlatformUtil } from '../../core/utils';
/**
* A shared service that registers component stylesheets exactly once per Angular injector,
* using `document.adoptedStyleSheets` in the browser and a `<style>` tag injection in SSR.
* A component in a shadow root passes its element, so that the stylesheet is also adopted
* by that shadow root.
*
* @example
* ```typescript
* // 1. Declare a unique symbol and import the generated CSS string at module level.
* const STYLES_ID = Symbol('igx-my-component');
*
* // 2. Call register() in the component/directive constructor.
* export class IgxMyComponent {
* constructor() {
* inject(IgxStylesRegistrar).register(STYLES_ID, MY_COMPONENT_CSS);
* // 2. Call register() once the element is attached, for example in ngOnInit.
* export class IgxMyComponent implements OnInit {
* private readonly styles = inject(IgxStylesRegistrar);
* private readonly element = inject(ElementRef);
*
* public ngOnInit(): void {
* this.styles.register(STYLES_ID, MY_COMPONENT_CSS, this.element.nativeElement);
* }
* }
* ```
Expand All @@ -41,29 +46,49 @@ export class IgxStylesRegistrar {
* `id` (against the same document) are no-ops, so it is safe to call from every
* component instance.
*
* The stylesheet is adopted by the document. When `host` is in a shadow root, the
* same stylesheet is also adopted by that shadow root, since styles of the document
* do not apply inside a shadow tree.
*
* @param id - A module-level `Symbol` that uniquely identifies the stylesheet.
* @param css - The CSS string to register (typically a generated `*.styles.ts` export).
* @param host - The element that uses the styles. Pass it once the element is attached,
* for example in `ngOnInit`, so that its shadow root can be found.
*
* @example
* ```typescript
* public ngOnInit(): void {
* inject(IgxStylesRegistrar).register(STYLES_ID, MY_COMPONENT_CSS, this.elementRef.nativeElement);
* }
* ```
*/
public register(id: symbol, css: string): void {
public register(id: symbol, css: string, host?: Node): void {
let registry = _registries.get(this._document);

if (!registry) {
registry = new Map<symbol, CSSStyleSheet | null>();
_registries.set(this._document, registry);
}

if (registry.has(id)) return;
if (!registry.has(id)) {
if (this._platform.isBrowser) {
const sheet = new CSSStyleSheet();
sheet.replaceSync(css);
registry.set(id, sheet);
this._document.adoptedStyleSheets = [...this._document.adoptedStyleSheets, sheet];
} else {
registry.set(id, null);
const style = this._document.createElement('style');
style.textContent = css;
this._document.head?.appendChild(style);
}
}

const sheet = registry.get(id);
const root = host?.getRootNode();

if (this._platform.isBrowser) {
const sheet = new CSSStyleSheet();
sheet.replaceSync(css);
registry.set(id, sheet);
this._document.adoptedStyleSheets = [...this._document.adoptedStyleSheets, sheet];
} else {
registry.set(id, null);
const style = this._document.createElement('style');
style.textContent = css;
this._document.head?.appendChild(style);
if (sheet && root instanceof ShadowRoot && !root.adoptedStyleSheets.includes(sheet)) {
root.adoptedStyleSheets = [...root.adoptedStyleSheets, sheet];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,12 +975,14 @@ export class IgxForOfDirective<T, U extends T[] = T[]> extends IgxForOfToken<T,U
* @hidden
* Function that is called when scrolling vertically
*/
protected onScroll(event: any) {
protected onScroll(_event: any) {
/* in certain situations this may be called when no scrollbar is visible */
if (!parseFloat(this.scrollComponent.nativeElement.style.height)) {
return;
}
this.scrollComponent.scrollAmount = event.target.scrollTop;
// Read the scroll element, not the event target: the grid handles the event after a throttle, when the
// browser has cleared the target of an event in a shadow tree.
this.scrollComponent.scrollAmount = this.scrollComponent.nativeElement.scrollTop;
if (!this._bScrollInternal) {
this._calcVirtualScrollPosition(this.scrollComponent.scrollAmount);
} else {
Expand Down Expand Up @@ -1182,13 +1184,14 @@ export class IgxForOfDirective<T, U extends T[] = T[]> extends IgxForOfToken<T,U
* @hidden
* Function that is called when scrolling horizontally
*/
protected onHScroll(event: any) {
protected onHScroll(_event: any) {
/* in certain situations this may be called when no scrollbar is visible */
const firstScrollChild = this.scrollComponent.nativeElement.children.item(0) as HTMLElement;
if (!parseFloat(firstScrollChild.style.width)) {
return;
}
this.scrollComponent.scrollAmount = event.target.scrollLeft;
// Read the scroll element, not the event target, which the browser clears after the dispatch in a shadow tree.
this.scrollComponent.scrollAmount = this.scrollComponent.nativeElement.scrollLeft;
if (!this._bScrollInternal) {
this._calcVirtualScrollPosition(this.scrollComponent.scrollAmount);
} else {
Expand Down Expand Up @@ -1868,8 +1871,10 @@ export class IgxGridForOfDirective<T, U extends T[] = T[]> extends IgxForOfDirec
}
}

public override onScroll(event: any) {
this.scrollComponent.scrollAmount = event.target.scrollTop;
public override onScroll(_event: any) {
// Read the scroll element, not the event target: the grid handles the event after a throttle, when the
// browser has cleared the target of an event in a shadow tree.
this.scrollComponent.scrollAmount = this.scrollComponent.nativeElement.scrollTop;
if (!this.scrollComponent.size) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,9 @@ export class IgxGridFilteringRowComponent implements OnInit, AfterViewInit, OnDe

// The 'iskeyPressed' flag is needed for a case in IE, because the input event is fired on focus and for some reason,
// when you have a japanese character as a placeholder, on init the value here is empty string .
const target = (eventArgs as InputEvent).target as HTMLInputElement;
// Read the input, not the event target: the debounced call runs after the dispatch, when the browser
// has cleared the target of an event in a shadow tree.
const target = this.input.nativeElement;
if (this.platform.isEdge && target.type !== 'number'
|| this.isKeyPressed || target.value || target.checkValidity()) {
this.value = target.value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ export abstract class IgxGridBaseDirective implements GridType,
/** @hidden @internal */
public i18nFormatter = inject(I18N_FORMATTER);
private readonly THROTTLE_TIME_MULTIPLIER = inject(SCROLL_THROTTLE_TIME_MULTIPLIER);
private readonly _styles = inject(IgxStylesRegistrar);
private throttleTime$ = new BehaviorSubject<number>(this.THROTTLE_TIME_MULTIPLIER);
/** @hidden @internal */
public throttleScheduler = animationFrameScheduler;
Expand Down Expand Up @@ -3622,9 +3623,7 @@ export abstract class IgxGridBaseDirective implements GridType,
}

constructor() {
const styles = inject(IgxStylesRegistrar);
styles.register(GRID_STYLES_ID, GRID_BASE_CSS);
styles.retain(
this._styles.retain(
IgxBadgeComponent, IgxChipComponent, IgxCheckboxComponent,
IgxInputGroupComponent, IgxDatePickerComponent, IgxCircularProgressBarComponent
);
Expand Down Expand Up @@ -4029,6 +4028,8 @@ export abstract class IgxGridBaseDirective implements GridType,
* @hidden
*/
public ngOnInit() {
// Registered once the grid is attached, so that a grid in a shadow root gets the styles in that shadow root.
this._styles.register(GRID_STYLES_ID, GRID_BASE_CSS, this.nativeElement);
this._setupServices();
this._setupListeners();
this.rowListDiffer = this.differs.find([]).create(null!);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import {
IgxGridFilteringMCHComponent,
IgxGridFilteringTemplateComponent,
IgxGridDatesFilteringComponent,
IgxGridFilteringNumericComponent
IgxGridFilteringNumericComponent,
IgxGridInShadowRootComponent
} from '../../../test-utils/grid-samples.spec';
import { ControlsFunction } from '../../../test-utils/controls-functions.spec';
import {
Expand Down Expand Up @@ -1846,6 +1847,30 @@ describe('IgxGrid - Filtering Row UI actions #grid', () => {
verifyChipVisibility(fix, 0, true);
});
});

describe('in a shadow root', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [IgxGridInShadowRootComponent],
providers: [{ provide: INPUT_DEBOUNCE_TIME, useValue: 0 }]
}).compileComponents();
});

it('should filter by the value typed in the filter row', async () => {
const fix = TestBed.createComponent(IgxGridInShadowRootComponent);
fix.detectChanges();
const grid = fix.componentInstance.grid;
expect(grid.nativeElement.getRootNode()).toBe(fix.nativeElement.shadowRoot);

GridFunctions.clickFilterCellChip(fix, 'Col0');
// The filter row handles the input after a debounce. The browser clears the target of
// an event in a shadow tree when the dispatch ends.
await typeFilterRowValue(fix, 'r1c0');

expect(grid.dataView.length).toBe(1);
expect(grid.dataView[0].ID).toBe(1);
});
});
});

/**
Expand Down
Loading
Loading