Render either a Sheet, or a Dialog in a streamlined way with Shadcn/ui?
Hi there, i'm dipping my toes into shadcn lately, and i'm really digging it. But a common issue i've been running into, is that often times i'd like to use a
Sheet
component on mobile, while on desktop i'd like to use a Dialog
component. Would there be a way to have a re-usable component that will either render a Dialog or a Sheet, similar to how the Sheet
and Dialog
function independently? Or do you think I am thinking of this in the wrong way? For context what i'm roughly thinking of:
11 Replies
I would have both on the page and only show one depending on the device width
with tailwind you can use
className="hidden md:flex"
and className="flex md:hidden"
for the different containersYeah, of course that would work, but that's not scalable for long-term in my opinion. You'd have so much repeated code for in essence, the exact same thing just a slightly different type of popup
I've did some brainstorming and came up with something like this, which I think is pretty okay(ish):
i would make a single component that renders both the dialogue and sheet like
Yeah that could also work, but that kind of disables all kind of props that I'd like to pass to the components from time to time
ya, there probably a way to get the intersecting properties and unique properties so you can do something like
<Sheet {...intersecting} {...sheetProps}>
and <Dialogue {...intersecting} {...dialogueProps}>
Hmm yeah. I appreciate your opinion on this! Not sure if this is kind of an anti-pattern I'm developing but we'll see haha
A type for getting properties common in all objects from a union
Using typescript type conditions and Exclude to get keys commmon in parts of a union and an object with only common keys from that union
something like
K in keyof T if K not keyof U
and K in keyof T if K keyof U
, im not a major typescript expert but chatgpt might be
i don't think its an antipattern, i've mostly just used the mobile dialogue because its works well enough
why do you want to render a sheet anyway?Well, I think sheets are just a way better experience on mobile than dialogs, dialogs feel very intrusive, and don't look as nice on mobile as sheets do.
fair enough
I think if you look at the Skateshop website (by shadcn) his way seems fine, I know what you said earlier but his implementation is pretty solid
he renders a sheet for mobile and something else for normal (can’t remember)