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

Snippet: Native style snap #2

Open
trasherdk opened this issue Nov 17, 2022 · 0 comments
Open

Snippet: Native style snap #2

trasherdk opened this issue Nov 17, 2022 · 0 comments

Comments

@trasherdk
Copy link
Owner

Use theme to enforce min-width in CSS when the sidebar is open.

Also calculate the "breakpoint" on window resize, to half the minimum width of the sidebar, converted to a percentage. Otherwise, the breakpoint would move when the window is resized.

<script>
  const sidebarMinWidth = 200 // in px
  let windowWidth // in px

  // Calculate "breakpoint" on window resize (half the width of the sidebar,
  // converted to a percentage of the window's inner width)
  let sidebarSnap = 0
  $: sidebarSnap = ((sidebarMinWidth / windowWidth) * 100) / 2

  let sidebarState = 'open'

  const onSidebarResize = (event) => {
    const { size, snap } = event.detail[0]
    if (sidebarState === 'open' && size < snap) {
      sidebarState = 'closed'
    }
    else if (sidebarState === 'closed' && size > snap) {
      sidebarState = 'open'
    }
  }
</script>

<svelte:window bind:innerWidth={windowWidth} />

<Splitpanes theme={sidebarState} on:resize={onSidebarResize}>
  <Pane size={0} minSize={0} maxSize={80} snapSize={sidebarSnap} class="sidebar"></Pane>
  <Pane></Pane>
</Splitpanes>

<style global lang="scss">
  .splitpanes.open .sidebar {
    min-width: 200px;
  }
</style>

Source: orefalo#37 (comment)

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

No branches or pull requests

1 participant