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

Modals when the svelte part of a website isn't full-width #835

Open
mrme44 opened this issue Oct 2, 2021 · 2 comments
Open

Modals when the svelte part of a website isn't full-width #835

mrme44 opened this issue Oct 2, 2021 · 2 comments
Labels
documentation Improvements or additions to documentation

Comments

@mrme44
Copy link

mrme44 commented Oct 2, 2021

When adding a modal, the modal is created within a position fixed element with a width of 100vw. This can make the modal run off the screen if there are any parent elements with position: relative that are not full-width, or any transform animations on a parent element that are not full-width.

One solution can be to make sure modal components are always placed at a top level in the DOM. That would work, except, Svelte can be embedded within a larger application. This is the problem I'm running into. There are 2 better solutions that would work:

Solution 1) Change the presentation element created by the modal to have a width and height of 100% instead 100vw/100vh. I can't think of any major backwards compatibility issues that would be created by doing this since right now the only way the component can be used as intended is when 100vw is the same as 100%.

Solution 2) Inject the modal element into either the body element or the root element of the svelte application. This can be done with the technique shown in this example.

Solution 1 would make the modal only show up over part of the screen, instead of full-width, when used in sub-components. Solution 2 would make sure the modal is always shown full-width. This is probably the better option.

@mrme44
Copy link
Author

mrme44 commented Oct 2, 2021

I have addressed this problem in my project by wrapping the modal component with the following component and using this instead:

<script>
import {onMount} from 'svelte';
import { Modal } from "carbon-components-svelte";
let modal;
onMount(()=>{
    document.body.appendChild(modal)
})
</script>

<div bind:this={modal}>
    <Modal {...$$restProps}>
        <slot />
    </Modal>
</div>

@brunnerh
Copy link
Contributor

brunnerh commented Oct 5, 2021

The logic of appending the content to the body can also be wrapped in a use action, that way you just need to add one simple wrapper and no additional components.

<script>
  import { portal } from './portal.js';
  // ...
</script>

<div use:portal={document.body}>
  <Modal .../>
</div>

REPL (made for sveltejs/svelte#1133)

@metonym metonym added the documentation Improvements or additions to documentation label Dec 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants