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

Add ability to set parent container #296

Open
G3EORG3E opened this issue Mar 12, 2024 · 6 comments · May be fixed by #356
Open

Add ability to set parent container #296

G3EORG3E opened this issue Mar 12, 2024 · 6 comments · May be fixed by #356

Comments

@G3EORG3E
Copy link

G3EORG3E commented Mar 12, 2024

The drawer dimensions are currently determined based on the viewport size. However, it would be beneficial to allow the specification of a container element for these calculations. This would enable the drawer to be used within a specific containing element, rather than occupying the entire screen.

@emilkowalski
Copy link
Owner

Can you give me an example where you'd nest it inside another element?

@G3EORG3E
Copy link
Author

I have two use cases:

  1. In the current project, there is a requirement for a fixed panel at the bottom of every page. The drawer component should be confined within the container above this panel, ensuring it never overlaps with the fixed panel. (so also the sizes of the drawer needs to be relative to that container)
  2. There is also a modal view. The drawer component should be integrated as a part of this modal view, rather than appearing over it.

Little sketch here:

image

@daranzhao
Copy link

daranzhao commented Mar 22, 2024

hi - I think this would have solved a similar use case where I define custom CSS variables (i.e. theme color) so that all components can be styled with the same tailwind classes (i.e. bg-theme) but with different variable values attached to them.

Since Radix's DialogPortal accepts a container, my workaround was to allow DrawerContent to take in an additional container prop and then pass that to DrawerPortal.

@jhgeluk
Copy link

jhgeluk commented Apr 12, 2024

hi - I think this would have solved a similar use case where I define custom CSS variables (i.e. theme color) so that all components can be styled with the same tailwind classes (i.e. bg-theme) but with different variable values attached to them.

Since Radix's DialogPortal accepts a container, my workaround was to allow DrawerContent to take in an additional container prop and then pass that to DrawerPortal.

Do you have the code of the workaround by any chance?

@daranzhao
Copy link

daranzhao commented Apr 18, 2024

I have something like this:

const DrawerContent = React.forwardRef<
  React.ElementRef<DrawerContentPrimitive>,
  React.ComponentPropsWithoutRef<DrawerContentPrimitive>
>(({ className, children, placement, theme, container, ...props }, ref) => (
  <DrawerPortal container={container}>
    ...
  </DrawerPortal>
))
DrawerContent.displayName = 'DrawerContent'

I wrap my app in a context provider that provides and sets the CSS variable in the container and then pass the element to places I want to use the drawer:

export function CustomStyledRadixContextProvider(props: {
  children: React.ReactNode
}) {
  const divRef = useRef<HTMLDivElement>(null)
  const [initialValue, setInitialValue] = useState<HTMLElement | undefined>()

  useLayoutEffect(() => {
    if (divRef.current) {
      setInitialValue(divRef.current)
    }
  }, [])

  const customStyles = {
    '--theme-color': color,
  } as React.CSSProperties

  return (
    <div style={customStyles} className="h-full w-full">
      <CustomStyledRadixContext.Provider value={initialValue}>
        {props.children}
      </CustomStyledRadixContext.Provider>
      <div ref={divRef} />
    </div>
  )
}
export function CustomStyledDrawerContent(props: {
  children: React.ReactNode
  [key: string]: any
}) {
  const radixStyleMountDiv = useContext(CustomStyledRadixContext)
  return (
    <DrawerContent
      container={radixStyleMountDiv}
      className="z-[100]"
      {...props}
    >
      {props.children}
    </DrawerContent>
  )
}

@RahulBirCodes
Copy link

RahulBirCodes commented May 20, 2024

Hi @emilkowalski, just wanted to followup on whether there's any sort of way I could reduce the calculations of vaul to be in a parent container's size. This is currently a pretty big bottleneck for me on desktop breakpoints where at the moment Im keeping the size of the app to be within a phone sized container.

@joaom00 joaom00 linked a pull request May 21, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants