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-tinacms-contentful

v0.7.15

Published

A React library for using Contentful with TinaCMS

Downloads

11

Readme

react-tinacms-contentful

A library for using Contentful with TinaCMS and React.js

Usage

Installation

To install the package, run:

npm install react-tinacms-contentful tinacms-contentful contentful contentful-management

Setup

To setup TinaCMS with Contentful, you must create an instance of the TinaCMS ContentfulClient for each space you want to edit content from.

For a single space:

import React, { useMemo } from 'React'
import { TinaProvider } from 'tinacms'
import { ContentfulClient } from 'tinacms-contentful'

export default function MyApp() {
  const cms = useMemo(() => {
    const contentful = new ContentfulClient({
      spaceId: /* Contentful Space ID */,
      defaultEnvironmentId: /* Contentful environment ID to use by default. Default: master */,
      accessTokens: {
        delivery: /* Contentful delivery access token for the space */,
        preview: /* Contentful preview access token for the space */,
      }
      clientId: /* OAuth App Client ID */,
      redirectUrl: /* OAuth App Callback URL */,
      rateLimit: /* API Rate Limit for your Contentful Plan (Requests per second). Default: 4 */,
      insecure: /* If true, uses same-site HTTPS cookies to create a session. Default: false */
    })

    return new TinaCMS({
      apis: {
        contentful
      }
    })
  });

  return (
    <TinaProvider cms={cms}>
      <TinaContentfulProvider>
        <div>Hello world</div>
      </TinaContentfulProvider>
    </TinaProvider>
  )
}

Or if the CMS has already been created:

import { useCMS } from 'tinacms'

export default function MyComponent() {
  const cms = useCMS();

  useEffect(() => {
    const contentful = new ContentfulClient({
      spaceId: /* Contentful Space ID */,
      defaultEnvironmentId: /* Contentful environment ID to use by default. Default: master */,
      accessTokens: {
        delivery: /* Contentful delivery access token for the space */,
        preview: /* Contentful preview access token for the space */,
      }
      clientId: /* OAuth App Client ID */,
      redirectUrl: /* OAuth App Callback URL */,
      rateLimit: /* API Rate Limit for your Contentful Plan (Requests per second). Default: 4 */,
      insecure: /* If true, uses same-site HTTPS cookies to create a session. Default: false */
    })

    cms.registerApi('contentful', contentful)
  }, [])

  return (
    <div>Hello world!</div>
  )
}

For multiple spaces:

import React, { useMemo } from 'React'
import { TinaProvider } from 'tinacms'
import { createContentfulClientForSpaces } from 'tinacms-contentful';

export default function MyApp() {
  const cms = useMemo(() => {
    const spaces = [
      {
        spaceId: /* Contentful Space ID */,
        defaultEnvironmentId: /* Contentful environment ID to use by default. Default: master */,
        accessTokens: {
          delivery: /* Contentful delivery access token for the space */,
          preview: /* Contentful preview access token for the space */,
        }
      },
      {
        spaceId: /* Contentful Space ID */,
        defaultEnvironmentId: /* Contentful environment ID to use by default. Default: master */,
        accessTokens: {
          delivery: /* Contentful delivery access token for the space */,
          preview: /* Contentful preview access token for the space */,
        }
      }
    ]

    const contentful = createClientForSpaces(spaces, {
      clientId: /* OAuth App Client ID */,
      redirectUrl: /* OAuth App Callback URL */,
      rateLimit: /* API Rate Limit for your Contentful Plan (Requests per second). Default: 4 */,
      insecure: /* If true, uses same-site HTTPS cookies to create a session. Default: false */
    })

    return new TinaCMS({
      apis: {
        contentful
      }
    })
  });

  return (
    <TinaProvider cms={cms}>
      <TinaContentfulProvider>
        <div>Hello world</div>
      </TinaContentfulProvider>
    </TinaProvider>
  )
}

Media

To add support for media, you must setup a media store for the space media should be uploaded to.

For a single space:

import React, { useMemo } from 'React'
import { TinaProvider } from 'tinacms'
import { ContentfulClient, ContentfulMediaStore } from 'tinacms-contentful'

export default function MyApp() {
  const cms = useMemo(() => {
    const contentful = new ContentfulClient({
      spaceId: /* Contentful Space ID */,
      defaultEnvironmentId: /* Contentful environment ID to use by default. Default: master */,
      accessTokens: {
        delivery: /* Contentful delivery access token for the space */,
        preview: /* Contentful preview access token for the space */,
      }
      clientId: /* OAuth App Client ID */,
      redirectUrl: /* OAuth App Callback URL */,
      rateLimit: /* API Rate Limit for your Contentful Plan (Requests per second). Default: 4 */,
      insecure: /* If true, uses same-site HTTPS cookies to create a session. Default: false */
    })
    const contentfulMediaStore = new ContentfulMediaStore(contentful)

    return new TinaCMS({
      apis: {
        contentful
      },
      media: contentfulMediaStore
    })
  });

  return (
    <TinaProvider cms={cms}>
      <TinaContentfulProvider>
        <div>Hello world</div>
      </TinaContentfulProvider>
    </TinaProvider>
  )
}

For multiple spaces:

The media store is only capable of acting on a single space at a time. To change spaces dynamically, run:

import React, { useMemo } from 'React'
import { TinaProvider } from 'tinacms'
import { ContentfulClient, ContentfulMediaStore } from 'tinacms-contentful'

+export default function MyApp(currentSpaceId) {
      const spaces = [
      {
        spaceId: /* Contentful Space ID */,
        defaultEnvironmentId: /* Contentful environment ID to use by default. Default: master */,
        accessTokens: {
          delivery: /* Contentful delivery access token for the space */,
          preview: /* Contentful preview access token for the space */,
        }
      },
      {
        spaceId: /* Contentful Space ID */,
        defaultEnvironmentId: /* Contentful environment ID to use by default. Default: master */,
        accessTokens: {
          delivery: /* Contentful delivery access token for the space */,
          preview: /* Contentful preview access token for the space */,
        }
      }
    ]

    const contentful = createClientForSpaces(spaces, {
      clientId: /* OAuth App Client ID */,
      redirectUrl: /* OAuth App Callback URL */,
      rateLimit: /* API Rate Limit for your Contentful Plan (Requests per second). Default: 4 */,
      insecure: /* If true, uses same-site HTTPS cookies to create a session. Default: false */
    })

+    const contentfulMediaStore = new ContentfulMediaStore(contentful[currentSpaceId])

    return new TinaCMS({
      apis: {
        contentful
      },
      media: contentfulMediaStore
    })
  });

  return (
    <TinaProvider cms={cms}>
      <TinaContentfulProvider>
        <div>Hello world</div>
      <TinaContentfulProvider>
    </TinaProvider>
  )
}

APIs

The library has the following core APIs:

  • ContentfulClient: an API client for communicating with Contentful that integrates directly with the CMS.
  • ContentfulMediaStore: a media store that uses a ContentfulClient to manage media for a single space.
  • Provider: a React provider for official plugins and authorization.
  • Hooks: a series of React Hooks for interacting with Contentful in your React app and CMS Plugins.

There are other public APIs as well. To learn more, read the full React API documentation or read the full JavaScript API documentation.

Provider

When using Contentful with TinaCMS and React, you must wrap the CMS-enabled portions of your app with the TinaContentfulProvider:

  return (
    <TinaProvider cms={cms}>
      <TinaContentfulProvider>
        <div>Hello world</div>
      <TinaContentfulProvider>
    </TinaProvider>
  )

Hooks

  • useContentful: retreives the ContentfulClient off of the CMS, and allows specifying a space ID when using multiple spaces.
  • useContentfulDelivery: retreives a delivery client from the ContentfulClient and allows specifying a space ID when using multiple spaces.
  • useContentfulPreview: retreives a preview client from the ContentfulClient and allows specifying a space ID when using multiple spaces.
  • useContentfulManagement: retreives a management client from the ContentfulClient and allows specifying a space ID when using multiple spaces.
  • useContentfulEntry: fetches an entry from contentful and returns the entry, loading constant, and error constant, and allows specifying a space ID when using multiple spaces.
  • useContentfulEntries: fetches multiple entries from contentful and returns the entry, loading constant, and error constant, and allows specifying a space ID when using multiple spaces.
  • useContentfulEntryForm: creates a TinaCMS form for a given entry, which can be registered with the CMS or used to provide editing UIs to end users.
  • useContentfulEntriesForm: creates a TinaCMS form for multiple entries, which can be registered with the CMS or used to provide editing UIs to end users.
  • useContentfulAuthRedirect: sets up a route to be used as the callback URL for an OAuth application.