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-share

v1.0.0

Published

Trigger the Web Share API, and fall back to copying the value to clipboard.

Downloads

134

Readme

useShare - your friendly web share API hook!

Trigger the Web Share API, and fall back to copying the value to clipboard.

Usage

This is how you use the useShare hook.

import React from 'react';
import useShare from 'use-share';

const ShareButton = () => {
  const { share, hasShared } = useShare({
    title: 'Check out use-share',
    text: 'use-share is a really cool library, you should check it out!',
    url: 'https://github.com/selbekk/use-share',
  });
  return (
    <button onClick={share} disabled={hasShared}>
      {hasShared ? 'Shared!' : 'Share'}
    </button>
  );
};

You call the hook with an object describing what you want to share - a title, a text and the URL to the content you want to share. You don't have to specify all three - but since you don't know how the user will want to share your content, you probably should. If the user chooses the e-mail app on their phone, for instance, they'll get the title as the subject, the text as the body, and a link to the URL at the bottom.

You can read more about the Web Share API at web.dev.

The hook returns an object with two properties - share and hasShared. When you call share, you share the content. hasShared is a boolean that is true for 1.5 seconds after you shared (so you can tell your user real quick).

Fallback

If the user's device doesn't support the Web Share API, it will copy the URL parameter to the clipboard. If you didn't specify a url parameter, it will copy the text, and if that isn't specified, it will copy the title. If you didn't specify any of the three, you should talk to somebody about your life choices.

No files?

The Web Share API supports sharing files too, but this hook doesn't. 🤷‍♂️ If you need support for this, use the Web Share API directly instead.

Questions?

Feel free to create an issue if you got questions, or reach out to me on Twitter. I ❤️ pull requests too, so if you find a bug, please let me know.