npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

use-start-view-transition

v1.0.2

Published

A set of hooks to use `document.startViewTransition` upon state/value changes

Downloads

1,139

Readme

use-start-view-transition

A set of hooks and utils to use document.startViewTransition upon state/value changes.

Demo & Playground

https://codesandbox.io/p/sandbox/hsrt6g

Installation

npm i use-start-view-transition

Usage

startViewTransition

A simply utility function that wraps the document.startViewTransition method. It supports server-side rendering and also gracefully falls back the method is not available in your browser.

import { startViewTransition } from 'use-start-view-transition'

startViewTransition(() => {
    // Your code here
})

useStartViewTransitionState

Straight up replacement for useState that starts a view transition upon state changes.

import { useStartViewTransitionState } from 'use-start-view-transition'

const [state, setState] = useStartViewTransitionState(0)

useStartViewTransitionWrap

A hook that can be used to wrap any tuples with the following shape: [state, setState]. It gives support to any state management library/util that uses the same shape.

import { useStartViewTransitionWrap } from 'use-start-view-transition'
import { useLocalStorage } from 'react-use'

const keyState = useLocalStorage('key', 0)
const [key, setKey] = useStartViewTransitionWrap(keyState)

// Or simply

const [key, setKey] = useStartViewTransitionWrap(useLocalStorage('key', 0))

useStartViewTransitionValue

A hook that lets you start a view transition upon a value has been changed. It's useful when you don't have access to the underlying state setter.

import { useStartViewTransitionValue } from 'use-start-view-transition'
import { useQuery } from 'react-query'

const { isLoading: _isLoading } = useQuery({})
const isLoading = useStartViewTransitionValue(_isLoading)

Options

All hooks accept an optional second argument that is an object with the following shape:

{
    skipTransition: boolean | (value => boolean)
}

skipTransition

A boolean or a function that returns a boolean. If true, the view transition will be skipped. If a function is provided, it will be called with the new value and should return a boolean. This is useful to handle cases where you don't want to start a view transition. For example, you only want a transition when the value is greater than 10.

const [state, setState] = useStartViewTransitionState(0, {
    skipTransition: value => value < 10
})

Asynchronous State Changes

There are cases when you'd like wait for the view transition to be done before doing something else. All hooks and utils are returning a promise that resolves when the view transition is done.

import { startViewTransition, useStarViewTransitionState } from 'use-start-view-transition'

const [state, setState] = useStartViewTransitionState(0)

const handleClick = async () => {
    await startViewTransition(() => {
        // Do something
    })

    // Do something else
}

const handleSelect = async value => {
    await setState(value)

    // Do something else
}

Controlling transition styles

https://developer.chrome.com/docs/web-platform/view-transitions/same-document

License

MIT