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

[data grid] Column Header tooltip should be displayed on keyboard focus (especially when a column has ellipsis) #13131

Open
shahidify opened this issue May 14, 2024 · 13 comments
Labels
accessibility a11y status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: commercial Support request from paid users support: pro standard Support request from a Pro standard plan user. https://mui.com/legal/technical-support-sla/

Comments

@shahidify
Copy link

shahidify commented May 14, 2024

The problem in depth

I am a MUI Pro license user.
I am working on an accessibility feature that needs to display column header tooltip on keyboard focus. This is essential in case where column header has ellipsis (due to resize or long header text). Datagrid tooltip is displayed only on mouse hover, not on keyboard hover. If I use custom header and update tabIndex, keyboard navigation gets trapped.

A similar issue in data cell got resolved using hasFocus prop but this prop is not available for header cell.

Example: https://codesandbox.io/p/sandbox/column-header-tooltip-on-keyboard-focus-s5h7rx

Any suggestion to add this behavior?

Your environment

Search keywords: Column Header tooltip, keyboard navigation
Order ID: 8813014139

@shahidify shahidify added status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: commercial Support request from paid users labels May 14, 2024
@michelengelen
Copy link
Member

Hey @shahidify
thanks for raising an issue.
The sandbox you linked is still private. Could you make it public so we can have a look?
thanks! 🙇🏼

@michelengelen michelengelen added accessibility a11y status: waiting for author Issue with insufficient information support: pro standard Support request from a Pro standard plan user. https://mui.com/legal/technical-support-sla/ and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels May 15, 2024
@michelengelen michelengelen changed the title Column Header tooltip should be displayed on keyboard focus (especially when a column has ellipsis) [data grid] Column Header tooltip should be displayed on keyboard focus (especially when a column has ellipsis) May 15, 2024
@shahidify
Copy link
Author

@michelengelen Sorry, I didn't realize it's private. Updated. It should work - https://codesandbox.io/p/sandbox/column-header-tooltip-on-keyboard-focus-s5h7rx

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels May 15, 2024
@michelengelen
Copy link
Member

This is basically a material-ui problem, since the Tooltip component apparently captures the keydown events without propagation. I am not super sure what happens here, but it works as expected when using a renderHeader without Tooltip.

@mnajdova what do you think? shouldn't this be in the material-ui repo?

@mnajdova
Copy link
Member

mnajdova commented May 16, 2024

The Tooltip components itself, by default shows the tooltip when the component receives a focus (the component in this case being the icon, no the cell). In order for this to work, the icon itself should have a tabIndex={0}, or be an IconButton component (or a custom button component).

I don't see a missing functionality on the Tooltip component iself, or please let me know if this is the case, I think this is more related to how the focus is being handled in the header cell.

Tooltip component apparently captures the keydown events without propagation

What do you mean, from what I tested it works as expected, see: https://codesandbox.io/p/sandbox/blue-sun-ng53wt?file=%2Fsrc%2FDemo.js%3A28%2C33.

@michelengelen michelengelen added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels May 16, 2024
@michelengelen
Copy link
Member

OK, I did some more investigation into this and the root issue was that there was a tabIndex={0} set on the Typography component. Possibly to show the Tooltip when it receives focus. At the same time this did trap the keyboard events.

I did come up with a different solution though:

const CustomColumnHeader: FunctionComponent<GridColumnHeaderParams> = ({ colDef, field }) => {
  const [isOpen, setIsOpen] = React.useState(false);
  const apiRef = useGridApiContext();
  const { headerName, description } = colDef;

  React.useEffect(() => {
    setIsOpen(apiRef.current?.state.focus.columnHeader?.field === field);
  }, [apiRef.current?.state.focus.columnHeader]);

  return (
    <Tooltip
      open={isOpen}
      title={
        <Box display="flex" flexDirection="column">
          <Typography sx={{ whiteSpace: 'pre-line' }}>{description}</Typography>
        </Box>
      }
    >
      <Typography variant="body2" sx={{ fontWeight: 'bold' }}>
        {headerName}
      </Typography>
    </Tooltip>
  );
};

Now the Tootip will show when the focus state of the headers change.
There is another possible solution that is more verbose:

React.useEffect(() => {
  apiRef.current.subscribeEvent('columnHeaderFocus', (params) => {
    if (field === params.field) {
      setIsOpen(true);
    }
  });
  apiRef.current.subscribeEvent('columnHeaderBlur', (params) => {
    if (field === params.field) {
      setIsOpen(false);
    }
  });
}, [apiRef]);

I do prefer the top one thoush.

@shahidify WDYT? Would that solve your issue?

@shahidify
Copy link
Author

Thank you @michelengelen
I will try this solution tomorrow and will let you know. Looks promising to me.

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels May 17, 2024
@michelengelen michelengelen added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels May 17, 2024
@shahidify
Copy link
Author

OK, I did some more investigation into this and the root issue was that there was a tabIndex={0} set on the Typography component. Possibly to show the Tooltip when it receives focus. At the same time this did trap the keyboard events.

I did come up with a different solution though:

const CustomColumnHeader: FunctionComponent<GridColumnHeaderParams> = ({ colDef, field }) => {
  const [isOpen, setIsOpen] = React.useState(false);
  const apiRef = useGridApiContext();
  const { headerName, description } = colDef;

  React.useEffect(() => {
    setIsOpen(apiRef.current?.state.focus.columnHeader?.field === field);
  }, [apiRef.current?.state.focus.columnHeader]);

  return (
    <Tooltip
      open={isOpen}
      title={
        <Box display="flex" flexDirection="column">
          <Typography sx={{ whiteSpace: 'pre-line' }}>{description}</Typography>
        </Box>
      }
    >
      <Typography variant="body2" sx={{ fontWeight: 'bold' }}>
        {headerName}
      </Typography>
    </Tooltip>
  );
};

Now the Tootip will show when the focus state of the headers change. There is another possible solution that is more verbose:

React.useEffect(() => {
  apiRef.current.subscribeEvent('columnHeaderFocus', (params) => {
    if (field === params.field) {
      setIsOpen(true);
    }
  });
  apiRef.current.subscribeEvent('columnHeaderBlur', (params) => {
    if (field === params.field) {
      setIsOpen(false);
    }
  });
}, [apiRef]);

I do prefer the top one thoush.

@shahidify WDYT? Would that solve your issue?

@michelengelen While both these solution work for keyboard focus, I observe column headers don't display tooltip on Mouse Hover anymore. Do we need to add some additional events for mouse hover ?

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels May 20, 2024
@michelengelen
Copy link
Member

Yes, you would need to add events for "columnHeaderOver"/"columnHeaderOut" or "columnHeaderEnter"/"columnHeaderLeave" ... whatever works best for you.

@michelengelen michelengelen added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels May 21, 2024
@httpete
Copy link

httpete commented May 21, 2024

Shouldn't the Grid do this by default? Having to work around it with a custom component doesn't seem proper.

@shahidify
Copy link
Author

I was out last week. I will try the suggested solution and will update. Thanks.

@github-actions github-actions bot removed the status: waiting for author Issue with insufficient information label May 28, 2024
@github-actions github-actions bot added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label May 28, 2024
@michelengelen
Copy link
Member

Shouldn't the Grid do this by default? Having to work around it with a custom component doesn't seem proper.

Not if you provide a custom renderer ...

@michelengelen michelengelen added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels May 28, 2024
@shahidify
Copy link
Author

Mouse hover and keyboard focus don't seem to work together. Multiple tooltips may open (one with keyboard, one with mouse hover).
Ex. - https://codesandbox.io/p/sandbox/column-header-tooltip-on-keyboard-focus-s5h7rx?file=%2Fsrc%2FCustomColumnHeader.jsx%3A25%2C8

@michelengelen any suggestion ?

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels May 28, 2024
@shahidify
Copy link
Author

Shouldn't the Grid do this by default? Having to work around it with a custom component doesn't seem proper.

Not if you provide a custom renderer ...

The original issue is to display tooltip on keyboard focus like how it works on mouse hover. I used custom renderer to achieve the same. Will be happy if it can be achieved without custom header.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accessibility a11y status: waiting for maintainer These issues haven't been looked at yet by a maintainer support: commercial Support request from paid users support: pro standard Support request from a Pro standard plan user. https://mui.com/legal/technical-support-sla/
Projects
None yet
Development

No branches or pull requests

4 participants