Giter Site home page Giter Site logo

daylilyfield / svrollbar Goto Github PK

View Code? Open in Web Editor NEW
75.0 2.0 12.0 7 MB

simple custom scrollbar made by svelte

Home Page: https://daylilyfield.github.io/svrollbar/

License: MIT License

JavaScript 70.44% Svelte 22.10% TypeScript 1.93% CSS 5.13% HTML 0.40%
scrollbar custom-scrollbar svelte svelte-component

svrollbar's Introduction

svrollbar

npm license actions coverall

svrollbar is the custom scrollbar made by svelte.

how to install

npm install svrollbar

examples

example website is here

example svelte REPL is here.

how to use

svrollbar has two components; Svrollbar.svelte and Svroller.svelte. svrollbar is supposed to use with svelte, but if you want, you can use svrollbar without svelte.

replace window scrollbar

if you would like to customize your window scrollbar, you simply write down Svrollbar.svelte with empty properties,

<Svrollbar />

this is equivalent to:

<Svrollbar viewport={document.scrollingElement} contents={document.body} />

yes, you can see the customized scrollbar on the right side of your browser window. please watch out example website to see the live example.

make scrollable area

if you try to make scrollable area within a part of your web site, you may prefer to use Svroller.svelte. the below example shows you the list which has 50 rows in a 10rem x 10rem square scrollable area with the custom scrollbar.

<script lang="ts">
  import { Svroller } from 'svrollbar'

  const items = Array.from({ length: 50 }).map((_, i) => `item ${i}`)
</script>

<Svroller width="10rem" height="10rem">
  {#each items as item (item)}
    <div>{item}</div>
  {/each}
</Svroller>

replace overflow-based scrollbar

on the other hand, it is better to use Svrollbar.svelte if you already have a kind of scrollable viewport or contents.

<script lang="ts">
  import { Svrollbar } from 'svrollbar'

  const items = Array.from({ length: 50 }).map((_, i) => `item ${i}`)

  export let viewport: Element
  export let contents: Element
</script>

<div class="wrapper">
  <div bind:this={viewport} class="viewport">
    <div bind:this={contents} class="contents">
      {#each items as item (item)}
        <div>{item}</div>
      {/each}
    </div>
  </div>
  <Svrollbar {viewport} {contents} />
</div>

<style>
  .wrapper {
    position: relative;
    width: 10rem;
  }

  .viewport {
    position: relative;
    width: 10rem;
    height: 10rem;
    overflow: scroll;
    border: 1px solid gray;
    box-sizing: border-box;

    /* hide scrollbar */
    -ms-overflow-style: none;
    scrollbar-width: none;
  }

  .viewport::-webkit-scrollbar {
    /* hide scrollbar */
    display: none;
  }
</style>

notice, you do not need to specify fixed value to width or height of viewport. you can set min-*, max-*, and any dynamic and reactive value because svrollbar observes both viewport size and its content size by ResizeObserver.

integrate 3rd party libraries

if you would like to integrate svrollbar into some kind of virtual list implemenation such as svelte-virtual-list or svelte-tiny-virtual-list, you can do that in the following way.

<script lang="ts">
  import { onMount } from 'svelte'
  import VirtualList from 'svelte-tiny-virtual-list'
  import { Svrollbar } from 'svrollbar'

  const items = Array.from({ length: 50 }).map((_, i) => `item ${i}`)

  let viewport: Element
  let contents: Element

  onMount(() => {
    viewport = document.querySelector('.virtual-list-wrapper')
    contents = document.querySelector('.virtual-list-inner')
  })
</script>

<div class="wrapper">
  <Svrollbar {viewport} {contents} />
  <VirtualList width="10rem" height={160} itemCount={items.length} itemSize={16}>
    <div slot="item" let:index let:style {style}>
      {items[index]}
    </div>
  </VirtualList>
</div>

<style>
  :global(.virtual-list-wrapper) {
    /* hide scrollbar */
    -ms-overflow-style: none !important;
    scrollbar-width: none !important;
  }

  :global(.virtual-list-wrapper::-webkit-scrollbar) {
    /* hide scrollbar */
    display: none !important;
  }

  .wrapper {
    position: relative;
    width: 10rem;
  }
</style>

components spec

see here.

how to customize transition

since the simple fade animation is really a bore, you can replace the default fade (show/hide) animation with your one. the transition function is compatible with the svelte transition.

<script>
  import { fly } from 'svelte/transition'
  import { Svroller } from 'svrollbar'

  const items = Array.from({ length: 50 }).map((_, i) => `item ${i}`)
  const flyLeft = (node) => fly(node, { x: -160 })
  const flyRight = (node) => fly(node, { x: 30 })
</script>

<Svroller
  width="10rem"
  height="10rem"
  vTrackIn={flyLeft}
  vTrackOut={flyLeft}
  vThumbIn={flyRight}
  vThumbOut={flyRight}>
  {#each items as item (item)}
    <div>{item}</div>
  {/each}
</Svroller>

how to customize style

you can customize svrollbar style with css variables.

variable default
--svrollbar-track-width 10px
--svrollbar-track-background initial
--svrollbar-track-radius initial
--svrollbar-track-opacity 1
--svrollbar-track-shadow initial
--svrollbar-thumb-width 6px
--svrollbar-thumb-background gray
--svrollbar-thumb-radius 0.25rem
--svrollbar-thumb-opacity 0.5
--svrollbar-thumb-shadow initial
<script lang="ts">
  import { Svroller } from 'svrollbar'

  const items = Array.from({ length: 50 }).map((_, i) => `item ${i}`)
</script>

<div class="container">
  <Svroller width="10rem" height="10rem">
    {#each items as item (item)}
      <div>{item}</div>
    {/each}
  </Svroller>
</div>

<style>
  .container {
    border: 3px solid #5d7150;
    width: 10rem;

    --svrollbar-track-width: 20px;
    --svrollbar-track-background: #85b4b9;
    --svrollbar-track-opacity: 1;

    --svrollbar-thumb-width: 10px;
    --svrollbar-thumb-background: #d9ab55;
    --svrollbar-thumb-opacity: 1;
  }
</style>

how to customize scrollbar visibility

you can customize scrollbar visibility with alwaysVisible and initiallyVisible properties.

property default description
alwaysVisible false scrollbar is always visible
initiallyVisible false scrollbar is visible until scrolling

svrollbar's People

Contributors

daylilyfield avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

svrollbar's Issues

Scrollbar appear on hover?

Thanks for the component.

Is there a way to make the scrollbar appear on contents hover if 100% of contents aren't visible i.e. if there's stuff to scroll to?

At present it seems the scrollbar only appears when user attempts to scroll but there's no indication, by way of a visible scrollbar, that the area is scrollable.

Option for alwaysVisible to only apply when area is scrollable

Following on from #4, it would be great if we had an option for alwaysVisible to only show the scrollbar if the area is scrollable. I guess you wouldn't want to change the default behaviour of the existing prop since that would be a breaking change, but I imagine you could overload it.

Eg:

  • alwaysVisible={true} current behaviour
  • alwaysVisible="scrollable" only visible if area is scrollable (content height > 100% of viewport height)
  • alwaysVisible="always" same as true

unmatch scrollbar thumb height in mobile chrome

i have two scrollbars on the right side of this window, one is the svrollbar and the other is the mobile chrome scrollbar. the svrollbar's scrollbar must match the height of the mobile chromes's one, but does not. this is because the auto-expanded navigation bar (i dont know the name of the view) may do bad things.

image

Visibility transition causing rerender issues between routes in Sveltekit

When initiallyVisible or alwaysVisible is true, Svrollbar doesn't properly rerender when changing routes in Sveltekit, causing layout bugs. This only occurs if the one of the visible override props are used, I assume due to the scrollbar not unmounting/updating in time (is there a rogue exit transition or something?)

Screen.Recording.2022-05-17.at.12.56.52.PM.mov

alwaysVisible doesn't dynamically update

Thanks for the excellent scrollbar library!

One minor issue, alwaysVisible doesn't seem to update when its value changes after the scrollbar has mounted/initialised.

Use-case for this: I want to show scrollbars when scrolling is available (ie: if inner content height > container height) but hide them when scrolling isn't available.

#4 would be another reasonable solution to this use-case, namely being that I need to give users some indication that a container is scrollable without them initiating a scroll.

Feature Request: Add track margin

Curious if you could add a track margin or padding option?

i.e. something like...

--svrollbar-track-margin: 10px;

I'm using the full page scrollbar and it touches the very top of the viewport.
image

document is not defined

Working with SvelteKit project which enabled SSR mode failed.

500
document is not defined
ReferenceError: document is not defined
    at Svrollbar.svelte:57:24
    at Object.$$render (PROJECT_HOME/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.js:1745:22)
    at index.svelte:235:27
    at Object.$$render (PROJECT_HOME/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.js:1745:22)
    at Object.default (root.svelte:43:47)
    at eval (/.svelte-kit/runtime/components/layout.svelte:8:41)
    at Object.$$render (PROJECT_HOME/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.js:1745:22)
    at root.svelte:37:45
    at $$render (PROJECT_HOME/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.js:1745:22)
    at Object.render (PROJECT_HOME/node_modules/.pnpm/[email protected]/node_modules/svelte/internal/index.js:1753:26)

The issue caused by no document object in SSR mode, and add a condition statement to fix it.

  // Svrollbar.svelte

  // line 56
  let windowScrollEnabled = false

  // Fixed: 'document is not defined' issue
  let ref = null
  if (ref) {
    $: windowScrollEnabled = document.scrollingElement === viewport
  }

  // line 234
  <div bind:this={ref} class="v-scrollbar" class:fixed={windowScrollEnabled} style="height: {trackHeight}px">
    <div
      bind:this={vTrack}
      class="v-track"
      style="height: {trackHeight}px"
      in:vTrackIn
      out:vTrackOut />
    <div
      bind:this={vThumb}
      class="v-thumb"
      style="height: {thumbHeight}px; top: {thumbTop}px"
      in:vThumbIn
      out:vThumbOut />
  </div>

Feature: Horizontal scroll

It seems Svrollbar do not support horizontal scroll at the moment.

It would be a great addition to the library!

make svrollbar disabled

svrollbar should be disabled.

let's say our app has some kind of dialog stuff that appears and covers the entire screen. the content behind the backdrop should not be scrolled.

<Svrollbar disabled={true} ... />
<Svroller disabled={true} ... />

Wrong height on iOS browsers

Hi!
I am using this component on my new project and I am very satisfied with the styling and the easy usage.

There is just one problem on the mobile browser versions on iOS. Seems like the browsers are returning wrong values for height properties. I tried the following browsers on my iPhone 12 mini with iOS version 15.5.

Chrome 103.0.5060.63
Firefox Daylight 102.1 (12709)
Safari 15

I added the svrollbar to my page with the following code snippet.
<Svrollbar alwaysVisible={true} initiallyVisible={true}></Svrollbar>

Here are some screenshots:

IMG_1567
IMG_1566
IMG_1565

Would be nice to discuss if there are some possible solutions for this behaviour.

Kind regards,
RenΓ©

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.