Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

use-contacts

Package Overview
Dependencies
Maintainers
0
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-contacts

Typesafe Contact Picker API wrapper for React

  • 1.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
15
decreased by-25%
Maintainers
0
Weekly downloads
 
Created
Source

useContacts

img img img img img

📇 Typesafe Contact Picker API wrapper for React

Implements the Contact Picker API into an easy-to-use React hook. This API is currently only available in Chromium based browsers and enabled on iOS Safari via a feature flag.

https://github.com/woofers/react-sheet-slide/assets/7284672/9ad2f50d-7c1e-451b-88e4-4f7ccb34ccb0

Features

  • Supports Server-Side rendering.
  • Test coverage with unit tests.
  • TypeScript support with type inference based on parameters.
  • Under 690 bytes GZipped.
  • Safely detect and fallback on unsupported browsers using isSupported method.
  • Cancels select when component is unmounted.
  • Provides explicit cancel method to abort the promise (contact picker still remains open).

Installation

pnpm

pnpm add use-contacts

Yarn

yarn add use-contacts

npm

npm install use-contacts

Usage

import React, { useCallback, useState } from 'react'
import { useContacts, hasContacts, isContactError } from 'use-contacts'

const App = () => {
  const { select, getProperties, isSupported, cancel } = useContacts()
  const selectAllContactValues = useCallback(() => {
    return select(['name', 'email', 'tel', 'icon', 'address'], { multiple: false })
  }, [select])
  const [contacts, setContacts] = useState<Awaited<ReturnType<typeof selectAllContactValues>>>([])
  const [error, setError] = useState<{ message: string }>()
  const selectContact = useCallback(() => {
    const updateContacts = async () => {
      try {
        const data = await selectAllContactValues()
        if (hasContacts(data)) {
          setContacts(data)
        }
      } catch (e) {
        if (isContactError(e)) setError(e)
      }
    }
    void updateContacts()
  }, [selectAllContactValues])
  const showSupportedProperties = useCallback(() => {
    const alertProperties = async () => {
      try {
        const props = await getProperties()
        if (typeof window !== 'undefined') {
          alert(`Supported contact fields are ${props.join(', ')}`)
        }
      } catch (e) {
        setError(e)
      }
    }
    void alertProperties()
  }, [getProperties])
  return (
    <>
      {hasContacts(contacts) && (
        <ul>
          {contacts.map(contact => (
            <li key={contact.name.join(' ')}>
              {contact.name} - {contact.tel}{' '}
              {contact.email && `(${contact.email})`}
            </li>
          ))}
        </ul>
      )}
      {isSupported() ? (
        <button type="button" onClick={selectContact}>
          Select a contact
        </button>
      ) : (
        <div>Contact Picker API not supported in this browser</div>
      )}
      {isSupported() && (
        <button type="button" onClick={showSupportedProperties}>
          Show supported fields on device
        </button>
      )}
      {!!error && <div>{error.message}</div>}
    </>
  )
}

Methods

select<T extends "name" | "address" | "email" | "icon" | "tel">(properties: [] | T[], options?: { multiple?: boolean }) =>
  Promise<Pick<{
    address?: ContactAddress[]
    email?: string[]
    icon?: Blob[]
    name: string[]
    tel?: string[]
  }, T>[] | []>'
  • select

    Opens the Contact Picker API in supported browsers and returns a promise which will resolve with an array of contacts.

    You can specify a list of properties as string keys using with the properties arg. If you pass an unsupported property, this will throw a Type error. If you want to auto-detect and use all supported properties call this without a properties argument or an empty array [].

    The options arg can be used to specify if the Contact Picker should be a multi-select using multiple.

    If you are using TypeScript, the correct array/contact object will be inferred from the arguments that you pass. For instance select(['name', 'tel'], { multiple: false }) will be typed as [{ name: string[]; tel?: string[] }].

  • getProperties() => Promise<("name" | "address" | "email" | "icon" | "tel")[]>'

    Returns the available properties supported by the current browser and device as an array.

  • cancel() => void

    Used to cancel the promise returned from select, it will resolve with an empty array of contacts. Otherwise if the promise is not pending, this performs a no-op. NOTE: The Contact Picker will still remain open as there is no way in any browser to close this programmatically, only the user can dismiss the Contact Picker however the promise will be resolved and cleaned up.

  • isSupported() => boolean

    Determines if the Contact Picker API is supported in the current browser.

Types

You import the following types from import type { Contact, SelectContact, ContactKey, ContactAddress } from 'use-contacts'

  • SelectContact<T extends ContactKey[] | ContactKey, K extends { multiple?: boolean }>

    Can be used to type useState by inferring or specifying the args passed to select.

    For instance SelectContact<'name' | 'tel', { multiple: true }> maps to { name: string[]; tel?: string[] }[].

Alternatively you can leverage the full-types as-well:

type Contact = {
  address?: ContactAddress[]
  email?: string[]
  icon?: Blob[]
  name: string[]
  tel?: string[]
}

type ContactKey = 'name' | 'address' | 'email' | 'icon' | 'tel'

type ContactAddress = {
  addressLine?: string[]
  city?: string
  country?: string
  dependentLocality?: string
  organization?: string
  phone?: string
  postalCode?: string
  recipient?: string
  region?: string
  sortingCode?: string
  toJSON: () => string
}

Keywords

FAQs

Package last updated on 09 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc