Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tab): trigger resize observer when label changes to make tab bar show scroll button correctly #480

Merged
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
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export type { StyleInfo };
export { FocusableHelper } from './utils/focusableHelper.js';
export { matches } from './utils/matches.js';
export { isBasicElement } from './utils/helpers.js';
export { triggerResize } from './utils/resizeHelper.js';

/**
* Export focused key.
Expand Down
11 changes: 11 additions & 0 deletions packages/core/src/utils/resizeHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* https://github.com/juggle/resize-observer/issues/42
*
* This event ensures that ResizeObserver picks up resize events
* when the element is deeply nested inside shadow root.
* TODO: remove this workaround once ResizeObserver handles shadow root scenario
* @returns {void}
*/
export const triggerResize = (): void => {
window.dispatchEvent(new Event('animationiteration'));
};
20 changes: 20 additions & 0 deletions packages/elements/src/tab-bar/__demo__/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,25 @@ <h6>Calendar</h6>
</ef-tab-bar>
<br>
</demo-block>

<demo-block header="Dynamic Rendering" layout="normal" tags="dynamicRendering">
<ef-tab-bar>
<ef-tab id="dynamic">tab 1</ef-tab>
<ef-tab id="dynamic">tab 2</ef-tab>
<ef-tab id="dynamic">tab 3</ef-tab>
<ef-tab id="dynamic">tab 4</ef-tab>
<ef-tab id="dynamic">tab 5</ef-tab>
<ef-tab id="dynamic">tab 6</ef-tab>
</ef-tab-bar>
<br>
</demo-block>
<script>
setTimeout(() => {
const tabs = document.querySelectorAll('#dynamic');
tabs.forEach((tab) => {
tab.textContent = 'some long label';
});
}, 5000);
</script>
</body>
</html>
8 changes: 8 additions & 0 deletions packages/elements/src/tab/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { customElement } from '@refinitiv-ui/core/decorators/custom-element.js';
import { property } from '@refinitiv-ui/core/decorators/property.js';
import { state } from '@refinitiv-ui/core/decorators/state.js';
import { isSlotEmpty } from '@refinitiv-ui/utils/is-slot-empty.js';
import { triggerResize } from '@refinitiv-ui/core';
import { VERSION } from '../version.js';

import '../icon/index.js';
Expand Down Expand Up @@ -132,6 +133,11 @@ export class Tab extends ControlElement {
if (changedProperties.has('active')) {
this.setAttribute('aria-selected', this.active ? 'true' : 'false');
}

if (changedProperties.has('label')) {
// ensures that tab-bar fire resizeCallback when label changes
triggerResize();
}
}

/**
Expand All @@ -142,6 +148,8 @@ export class Tab extends ControlElement {
private onSlotChange = (event: Event): void => {
const slot = event.target as HTMLSlotElement;
this.isSlotHasContent = !!slot.assignedNodes().length && isSlotEmpty(slot);
// ensures that tab-bar fire resizeCallback when slot changes
triggerResize();
};

/**
Expand Down