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(tabs): add fallback values for element width values #479

Merged
merged 3 commits into from
Feb 8, 2023
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
Expand Up @@ -101,7 +101,7 @@ export const PrimaryTabList = ({
))}

{collapsedOptions.length ? (
<span ref={addonRef} role='tablist' className={styles.pickerWrapper}>
<span ref={addonRef} role='menu' className={styles.pickerWrapper}>
<PickerButtonDesktop
fieldClassName={styles.title}
optionClassName={cn(styles.pickerOption, size && styles[size])}
Expand Down
14 changes: 5 additions & 9 deletions packages/tabs/src/hooks/use-collapsible-elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,18 @@ export const useCollapsibleElements = <
if (!container) return;

const addon = addonRef.current;
const moreElement = (
Array.from(container.querySelectorAll('[role="tablist"]')) as HTMLElement[]
).pop();
const moreElementRect = moreElement?.getBoundingClientRect();
const elements = Array.from(container.querySelectorAll(selectors)) as HTMLElement[];
const containerWidth =
(inlineSize || container.clientWidth) - (moreElementRect?.width || 0) * 1.5; // при рассчётах, даём кнопке "Ещё" чуть больше места, чтобы точно влезла
(inlineSize || container.clientWidth) - (addon?.scrollWidth || 0) * 1.5; // при расчётах, даём кнопке "Ещё" чуть больше места, чтобы точно влезла
const elements = Array.from(container.querySelectorAll(selectors)) as HTMLElement[];

const collapsedIds = elements.reduce<string[]>((acc, element) => {
const { offsetLeft, offsetWidth, id } = element;
const elementOffset = offsetLeft + offsetWidth;
const { offsetLeft, scrollWidth, id } = element;
const elementOffset = offsetLeft + scrollWidth;
const isCollapsedElement = getComputedStyle(element).visibility === 'collapse';
const maxWidth =
addon && !isCollapsedElement
? containerWidth -
(addon.offsetWidth + parseFloat(getComputedStyle(addon).marginLeft))
(addon.scrollWidth + parseFloat(getComputedStyle(addon).marginLeft))
: containerWidth;

if (elementOffset >= maxWidth) acc.push(id);
Expand Down
2 changes: 1 addition & 1 deletion packages/tabs/src/hooks/use-tablist-titles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const useTablistTitles = ({
const { containerRef, addonRef, idsCollapsedElements } = useCollapsibleElements<
HTMLDivElement,
HTMLInputElement
>('[role=tab]', [titles]);
>('[role=tab]', [titles, collapsedTabsIds]);

const [view] = useMedia<TabsMatchMedia>(
[['desktop', `(min-width: ${breakpoint}px)`]],
Expand Down