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 dropdown cutoff issue for long text in mobile view (#7035) #7039

Closed
wants to merge 3 commits into from
Closed
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
4 changes: 4 additions & 0 deletions apps/site/components/Common/Select/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@
dark:data-[disabled]:text-neutral-700;
}

.textWrap {
text-wrap: wrap !important ;
}
Comment on lines +136 to +138
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution 🙇

As Claudio stated, I also think we should solve these problems in CSS

We use Tailwind classes to style CSS files;
https://github.com/nodejs/nodejs.org/blob/main/COLLABORATOR_GUIDE.md#example-of-a-css-module

In addition, we are careful not to define important properties and classes unless necessary.

You can make an example arrangement like this;

.inline {
  ... some code

  .text span > span {
    @apply max-w-fit
      text-wrap;
  }
}


&.dropdown {
@apply mt-1
w-[calc(100%+1.5rem)]
Expand Down
32 changes: 29 additions & 3 deletions apps/site/components/Common/Select/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ChevronDownIcon } from '@heroicons/react/24/outline';
import * as ScrollPrimitive from '@radix-ui/react-scroll-area';
import * as SelectPrimitive from '@radix-ui/react-select';
import classNames from 'classnames';
import { useId, useMemo } from 'react';
import { useEffect, useId, useMemo, useRef, useState } from 'react';
import type { FC } from 'react';

import type { FormattedMessage } from '@/types';
Expand Down Expand Up @@ -51,7 +51,9 @@ const Select: FC<SelectProps> = ({
ariaLabel,
}) => {
const id = useId();

const spanRef = useRef<HTMLSpanElement>(null);
const [elementWidth, setElementWidth] = useState(0);
const [windowWidth, setWindowWidth] = useState(window.innerWidth);
const mappedValues = useMemo(() => {
let mappedValues = values;

Expand All @@ -66,13 +68,32 @@ const Select: FC<SelectProps> = ({
return mappedValues;
}, [values]);

useEffect(() => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like a very inefficient way of solving an issue that could be solved only with CSS.

const handleResize = () => {
setWindowWidth(window.innerWidth);
};

window.addEventListener('resize', handleResize);

return () => {
window.removeEventListener('resize', handleResize);
};
}, []);

useEffect(() => {
if (spanRef.current) {
setElementWidth(spanRef.current.offsetWidth);
}
}, [windowWidth]);

return (
<span
className={classNames(
styles.select,
{ [styles.inline]: inline },
className
)}
ref={spanRef}
>
{label && (
<label className={styles.label} htmlFor={id}>
Expand All @@ -94,6 +115,9 @@ const Select: FC<SelectProps> = ({
<SelectPrimitive.Content
position={inline ? 'popper' : 'item-aligned'}
className={classNames(styles.dropdown, { [styles.inline]: inline })}
style={{
maxWidth: `${elementWidth}px`,
}}
>
<ScrollPrimitive.Root type="auto">
<SelectPrimitive.Viewport>
Expand All @@ -117,7 +141,9 @@ const Select: FC<SelectProps> = ({
>
<SelectPrimitive.ItemText>
{iconImage}
<span>{label}</span>
<span className={classNames(styles.textWrap)}>
{label}
</span>
</SelectPrimitive.ItemText>
</SelectPrimitive.Item>
))}
Expand Down