-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Tooltip trigger onMobile #86
Comments
Also the tooltip only opens when you hover the border, does not work on the surface: E.g here: https://ui.shadcn.com/docs/primitives/tooltip |
This is a design decision from Radix UI team, see #955 and the answer by Benoît Grélard.
In case you "need a tooltip" use a popover instead, as they do in their documentation. |
tooltip Not work on mobile view |
any progress? |
@0xbid the only solution is described above. So i use Tooltip for Desktop and for tablet and mobile i just change it to popover. Or u can make some own logic instead of using libraries(made one as well). |
To summarise, they are three components: Tooltip and Hover Card are only meant to be used on desktop. Popover is meant to be used on desktop and mobile. This is a design choice by the Radix people. I think it wouldn't make sense to change it here at Shadcn-UI 🤔 so I reckon this issue can be closed. If you need a hybrid solution, which mounts a popover on mobile and a tooltip by default, have a look at this suggestion 😉 |
Does it continue to make sense to be locked-in to radix for this scenario? I mean the Radix design choices shouldn't limit the capabilities offered by ShadCn. |
I am surprised, that in 2024, we are still trying to differentiate between "desktop", "touch", "pointer down". When do we realize that everything is hybrid? We will never know if a user is touching only and within the next minute using a pointer device on the same screen? Simple example: I am on an iPad with an iPencil, how should the tooltip behave? Shouldn't responsive components always work consistently on any "device" with and without "touch"? 🙏 |
I ended up using @floating-ui/react Here is the demo I found and used as the template for my own re-usable component. It works great on touch devices. https://codesandbox.io/p/sandbox/xenodochial-grass-js3bo9?file=%2Fsrc%2FTooltip.tsx In case you are curious here is a gist with the component I build: |
This is what I have settled for. I know not the most elegant but mostly works. |
This is the correct solution, and it works like a charm. But honestly I don't understand the point of having both the |
This issue has been automatically closed because it received no activity for a while. If you think it was closed by accident, please leave a comment. Thank you. |
Here is a tooltip that works on both mobile and desktop. We just handle all interactions ourselves. cc: @shadcn const Tip = ({
content,
children,
className
}: React.PropsWithChildren<{ content: string | React.ReactNode; className?: string }>) => {
const [open, setOpen] = React.useState(false);
return (
<TooltipProvider delayDuration={0}>
<Tooltip open={open}>
<TooltipTrigger asChild>
<button
type="button"
className={cn('cursor-pointer', className)}
onClick={() => setOpen(!open)}
onMouseEnter={() => setOpen(true)}
onMouseLeave={() => setOpen(false)}
onTouchStart={() => setOpen(!open)}
onKeyDown={(e) => {
e.preventDefault();
e.key === 'Enter' && setOpen(!open);
}}
>
{children}
</button>
</TooltipTrigger>
<TooltipContent className={!content ? 'hidden' : ''}>
<span className="inline-block">{content}</span>
</TooltipContent>
</Tooltip>
</TooltipProvider>
);
}; |
@devotox thx for the solution!
|
@cherkanovart for me the reason I did it the other way was I want it to be a toggle so that clicking or touching can open and close the tooltip |
I wish the tooltip worked on mobile devices as well. Since all Shadcn UI components and the Tailwind framework support various dimensions, including tablets, pc, and mobile devices, all features should be compatible across all devices. So I suggest reopening the issue @shadcn |
I wrote a simple solution that works fine for me so it can work both mouse and touchscreen import * as TooltipPrimitive from "@radix-ui/react-tooltip"
import React from "react"
export const TooltipRoot = React.forwardRef<
HTMLDivElement,
React.ComponentPropsWithoutRef<typeof TooltipPrimitive.Root> & {
useTouch?: boolean;
useTap?: boolean;
}
>(({ useTouch = false, useTap = false, children, ...props }, _ref) => {
const [open, setOpen] = React.useState(false);
const handleTouch = (event: React.TouchEvent | React.MouseEvent) => {
event.persist();
setOpen(true);
};
return (
<TooltipPrimitive.Root
open={open}
onOpenChange={setOpen}
{...props}
>
{React.Children.map(children, (child) => {
if (React.isValidElement(child) && useTouch) {
return React.cloneElement(child as React.ReactElement<any>, {
onTouchStart: handleTouch,
onMouseDown: handleTouch,
});
}
return child;
})}
</TooltipPrimitive.Root>
);
});
TooltipRoot.displayName = TooltipPrimitive.Root.displayName; usage: <TooltipRoot useTouch={true}>
<TooltipTrigger asChild>
</TooltipRoot> |
I would like to ask how does tooltip work on mobile devices? Because when i tried to use it there it does not work. Is there some solution for this to trigger tooltip when user clicks on trigger?
The text was updated successfully, but these errors were encountered: