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

react-anchor-navigation

v0.2.7

Published

React lightweight library for anchor scrolling and navigation tied to URL hash.

Downloads

3,479

Readme

Logo

react-anchor-navigation

npm type definitions

Cypress.io

lint

e2e

Description

This library exports multiple helpers designed to make your anchor navigation works seamlessly. Check the examples

📖 table of content

🚀 Installation

This project uses react hooks and is therefore reliant on react version >=16.8.6

To install and use react-anchor-navigation, add it to your package.json and modules with the following command line :

npm install --save react-anchor-navigation

OR

yarn add react-anchor-navigation

🖥️ How to use

Enregistrement de l’écran 2023-06-01 à 12 12 50

import {
  AnchorContext,
  AnchorLink,
  AnchorProvider,
  AnchorSection,
} from "react-anchor-navigation";

AnchorProvider

AnchorProvider is our top level contextProvider. Wrap it around your topmost component for your view :

<AnchorProvider>
  <YourView />
</AnchorProvider>

| Key | Type | Description | | ----------- | :-----------------: | ---------------------------------------------------------------------------------------------------------------: | | offset | number | The offset amount of pixels from the top, usefull when handling fixed header or sticky navigation (default: 0) | | getScroller | () => HTMLElement | Function to returns the scrollable element (default: body) |

It will provide the AnchorContext to all children.

AnchorContext

AnchorContext is the context you can yield with the new useContext hook or with old-fashioned Context.consumer.

const { hash, sections } = useContext(AnchorContext);

Here is its typing :

| Key | Type | Description | | ----------------- | :---------------------------------------------: | -------------------------------------------------------------------------------------------------------------------------------: | | sections | HTMLElement[] | List of the registered sections elements that we watch, in our codebase it is AnchorSection | | hash | string | The registered hash corresponding to our current scroll position | | registerSection | (element: HTMLElement) => void | Function to add a Section to our sections list, our scrollEvent listener will then update the hash if the section is scrolled to | | unregisterSection | (element: HTMLElement) => void | Function to remove a Section to our sections list, our scrollEvent listener will then stop checking this section | | setHash | (hash: string, withScroll?: boolean) => void; | Setter function from the internal useHash hooks, use it to programmatically change the current hash | | offset | number | The offset amount of pixels from the top, usefull when handling fixed header or sticky navigation (default: 0) |

AnchorSection

AnchorSection is our most used components, it defines the scroll position you will arrive to on navigation/reloading

<AnchorSection className="dBlock anchor" id="definitions" />

Its props are the usual HTMLElement's props (className, data-*), along with an id used for recognizing the update the current hash on scroll.

interface TProps extends React.HTMLAttributes<HTMLElement> {
  id: string;
}

Internally it creates a <b/> tag to which we scroll to on reload and detect if we scrolled past it.

<>
  <b {...attributes} />
  {...children}
</>

AnchorLink

AnchorLink is a component made to have an activeClassname if its href is the current hash

interface TProps extends React.AnchorHTMLAttributes<HTMLAnchorElement> {
  children: React.ReactNode[] | React.ReactNode;
  activeClassName?: string;
}
<AnchorLink className="dTable w100 pad15" href="#definitions" activeClassName="blue">

⏳ How to test

react-anchor-navigation can be tested with the end-to-end testing library Cypress.

To run the tests, run yarn cypress and select the test specs to run in the Cypress window.

Learn more about writing your own Cypress tests with the Cypress documentation.

🤝 How to contribute

  • fork the project

  • install dependencies (yarn)

  • create a branch from main/master like that

    $ contribution/fix/your-github-identity

    OR

    $ contribution/improvment/your-github-identity
  • push several (if needed) clear commits

  • add tests following the way of the other ones have been wrote

  • make sure that all test runs

  • push your code

📦 List of our other package

⛵ Join us

May you want to share more than a pull request check our jobs opportunity

License

Copyright (c) 2023 Koala-Interactive

This project is MIT licensed.