Git Product home page Git Product logo

compute-scroll-into-view's Introduction

npm stat npm version gzip size size semantic-release

compute-scroll-into-view

Lower level API that is used by the ponyfill scroll-into-view-if-needed to compute where (if needed) elements should scroll based on options defined in the spec and the scrollMode: "if-needed" draft spec proposal. Use this if you want the smallest possible bundlesize and is ok with implementing the actual scrolling yourself.

Scrolling SVG elements are supported, as well as Shadow DOM elements. The VisualViewport API is also supported, ensuring scrolling works properly on modern devices. Quirksmode is also supported as long as you polyfill document.scrollingElement.

Install

npm i compute-scroll-into-view

You can also use it from a CDN:

const { compute } = await import('https://esm.sh/compute-scroll-into-view')

Usage

import { compute } from 'compute-scroll-into-view'

const node = document.getElementById('hero')

// same behavior as Element.scrollIntoView({block: "nearest", inline: "nearest"})
// see: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView
const actions = compute(node, {
  scrollMode: 'if-needed',
  block: 'nearest',
  inline: 'nearest',
})

// same behavior as Element.scrollIntoViewIfNeeded(true)
// see: https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoViewIfNeeded
const actions = compute(node, {
  scrollMode: 'if-needed',
  block: 'center',
  inline: 'center',
})

// Then perform the scrolling, use scroll-into-view-if-needed if you don't want to implement this part
actions.forEach(({ el, top, left }) => {
  el.scrollTop = top
  el.scrollLeft = left
})

API

compute(target, options)

options

Type: Object

Type: 'start' | 'center' | 'end' | 'nearest'
Default: 'center'

Control the logical scroll position on the y-axis. The spec states that the block direction is related to the writing-mode, but this is not implemented yet in this library. This means that block: 'start' aligns to the top edge and block: 'end' to the bottom.

Type: 'start' | 'center' | 'end' | 'nearest'
Default: 'nearest'

Like block this is affected by the writing-mode. In left-to-right pages inline: 'start' will align to the left edge. In right-to-left it should be flipped. This will be supported in a future release.

Type: 'always' | 'if-needed'
Default: 'always'

This is a proposed addition to the spec that you can track here: w3c/csswg-drafts#5677

This library will be updated to reflect any changes to the spec and will provide a migration path. To be backwards compatible with Element.scrollIntoViewIfNeeded if something is not 100% visible it will count as "needs scrolling". If you need a different visibility ratio your best option would be to implement an Intersection Observer.

Type: Element | Function

By default there is no boundary. All the parent elements of your target is checked until it reaches the viewport (document.scrollingElement) when calculating layout and what to scroll. By passing a boundary you can short-circuit this loop depending on your needs:

  • Prevent the browser window from scrolling.
  • Scroll elements into view in a list, without scrolling container elements.

You can also pass a function to do more dynamic checks to override the scroll scoping:

const actions = compute(target, {
  boundary: (parent) => {
    // By default `overflow: hidden` elements are allowed, only `overflow: visible | clip` is skipped as
    // this is required by the CSSOM spec
    if (getComputedStyle(parent)['overflow'] === 'hidden') {
      return false
    }

    return true
  },
})

skipOverflowHiddenElements

Type: Boolean
Default: false

By default the spec states that overflow: hidden elements should be scrollable because it has been used to allow programatic scrolling. This behavior can sometimes lead to scrolling issues when you have a node that is a child of an overflow: hidden node.

This package follows the convention adopted by Firefox of setting a boolean option to not scroll all nodes with overflow: hidden set.

compute-scroll-into-view's People

Contributors

92thunder avatar eaglus avatar erictaylor avatar github-actions[bot] avatar ichuyko avatar li-jia-nan avatar long76 avatar prettierci[bot] avatar pvojtechovsky avatar renovate[bot] avatar semantic-release-bot avatar shiftonetothree avatar stipsan avatar

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.