npm package: @phantomstudios/ft-react-components
Description: A collection of UI/utility React/NextJS components for Phantom FT sites
Author: Dhia Shakiry
Latest version: 0.3.2

# ft-react-components

[![NPM version][npm-image]][npm-url]
[![Actions Status][ci-image]][ci-url]
[![PR Welcome][npm-downloads-image]][npm-downloads-url]

A collection of React/NextJS UI and utility components for Phantom FT sites.

## Introduction

Currently implemented:

- FTTracker - Provides a context and hook for easier access to the FTTracker and ReactPlayerTracking tracking functions from @phantomstudios/ft-lib.
- FTCookieMessage - Wrapper loader for the standard FT origami cookie consent popup
- FTFooter - Renders the standard FT partner site footer
- FTHeader - Renders the standard FT partner site header.
- FTPartnerContent - Renders the FT partner header
- ShareButton - used in the FTPartnerContent component
- PermutiveSetup - Outputs the Permutive tracking scripts for FT partner sites.
- PreviewMessage - a styled component to display a preview mode indicator for sites using Directus CMS.
- BrandMetricsSurvey - A component that loads the BrandMetrics script and survey to the page.
- TrackingPixel - A styled component for (hidden) tracking pixels.

## Installation

Install this package with `npm`.

```bash
npm i @phantomstudios/ft-react-components
```

## Usage

### FTTracker

Add the provider to \_app.tsx, pass the site's page content to PageViewData

```JSX
import { FTTrackerProvider } from "@phantomstudios/ft-react-components";
.
.
.
return (
<FTTrackerProvider>
    <PageViewData pageProps={pageProps} />
    <Component {...pageProps} />;
  </FTTrackerProvider>
)
```

Construct the pageViewData object from the site content data. use setPageViewData and setTrackingOptions to update the tracker provider.
See the Phantom FT partner tracking implementation guide for the full config object schema.
TODO - consider moving <PageViewData> to this lib with empty/default values and adding Yup validation to console error/eslint when incomplete.

```JSX
import { useFTTracker, TrackingOptions, ConfigType } from "@phantomstudios/ft-react-components";
.
.
const { setPageViewData, setTrackingOptions } = useFTTracker();

 useEffect(() => {
    const globals = pageProps.globals;
    const article = pageProps.article;
    const category = pageProps.category;

    setTrackingOptions({
      scrollTrackerSelector: "#o_tracker_scroll",
      isCustomGTMEvent: true,
      sendGTMPageViewYN: true,
    });

      const errorValue = "404";
    if (!setPageViewData) return;
    setPageViewData({
      advertiserIndustry: (globals && globals.sitewide.industry) || errorValue,
      app:
        (article && article.content_type) ||
        (globals && globals.sitewide.homepage_label) ||
        errorValue,
      articleName:
        (article && article.title) ||
        (globals && globals.sitewide.homepage_label) ||
        errorValue,
      author:
      .
      .
      .
      } as ConfigType);
    }, [pageProps]);
```

#### Video tracking

Video event tracking is handled by the videoTracker instance available from the `useFTTracker` hook. It is currently only compatible with `react-player` implementations which is the current standard player implementation for Phantom NextJS sites.

```JSX
import { useFTTracker } from "@phantomstudios/ft-react-components";
import ReactPlayer from "react-player";
.
.
const { videoTracker } = useFTTracker();
.
.
<ReactPlayer
  controls
  height="100%"
  style={{ position: "absolute", top: 0, left: 0 }}
  onDuration={videoTracker?.setDuration}
  onEnded={videoTracker?.trackEnded}
  onPause={videoTracker?.trackPause}
  onPlay={videoTracker?.trackPlay}
  onProgress={videoTracker?.trackProgress}
  width="100%"
  url={fileToURL(article.video)}
  light={fileToURL(article.image)}
  playIcon={<PlayIconCircle />}
  onClick={handlePlayButtonClick}
/>
```

### PermutiveSetup

Loads the external permutive scripts, requires Permutive project specific IDs (environment variables) - add to the sites' `_document.tsx`

```JSX
import { PermutiveSetup } from "@phantomstudios/ft-react-components";
.
.
// Permutive tracking
const PERMUTIVE_PROJECT_ID = process.env.PERMUTIVE_PROJECT_ID || "";
const PERMUTIVE_PUBLIC_API_KEY = process.env.PERMUTIVE_PUBLIC_API_KEY || "";
.
.
render() {
    return (
      <Html lang="en">
        <Head>
          <FontPreloadEmbed />
          <FaviconEmbed />
          <TrackingHeadScript id={GA_TRACKING_ID} />
          <PermutiveSetup
            projectID={PERMUTIVE_PROJECT_ID}
            publicAPIKey={PERMUTIVE_PUBLIC_API_KEY}
          />
.
.
```

### FTCookieMessage, FTFooter, FTHeader, FTPartnerContent reuseable components

Render in an appropriate page level component. FTPartnerContent requires 3 props as below to display the site's required partner links. It has optional `disclaimer` (controls the on/off tooltip state) and `author` (determines 1 of 4 tooltip texts) props.

```JSX
import {
  CookieMessage,
  FTHeader,
  FTFooter,
  FTPartnerContent,
} from "@phantomstudios/ft-react-components";
.
.
  <Container>
        <FTHeader />
        <FTPartnerContent
          siteUrl={globals.sitewide.url}
          siteTitle={globals.sitewide.title}
          partner={{
            title: "Zurich",
            url: "https://www.zurich.com/",
          }}
        />
        {showHero && <CategoryHero />}
        <Navbar categories={globals.categories} selected={category} />
        <div id="o_tracker_scroll">{children}</div>
        <CookieMessage />
        <FTFooter />
      </Container>
```

#### Reuseable component styles and icons

NOTE: all 4 components rely on a single global .scss styles file, which needs to be loaded in `_app.tsx` as a global import:

```JSX
//load combined FT components styles as global
import "@phantomstudios/ft-react-components/src/components/ft/styles/app.scss";
```

Additionally, some the components use styling from the @financial-times root components, so adding a node_modules sass path to NextJS's `next.config.js` is required:

```JS
module.exports = {
  //other config
  sassOptions: {
    includePaths: [
      // Required by the @financial-times/o-<component> sass
      path.resolve(__dirname, "./node_modules/@financial-times"),
    ],
  },
};
```

Currently, the partner header icons need to be copied to the NextJS sites' /public/ft/partner-content folder

### BrandmetricsSurvey

Implements the placeholder div and initialisation for Brandmetrics surveys.
NOTE: Surveys also require the separate Brandmetrics external CDN script to be loaded and available on `window`.

```JSX
import { BrandMetricsSurvey } from "@phantomstudios/ft-react-components";
.
.
{article.brandmetrics_survey_id && (
  <BrandMetricsSurvey surveyId={article.brandmetrics_survey_id}
  />
)}
```

### TrackingPixel

Renders a styled img with with display:hidden and height:0

```JSX
import { TrackingPixel } from "@phantomstudios/ft-react-components";
.
.
{article.brandmetrics_pixel_url && (
  <TrackingPixel src={article.brandmetrics_pixel_url} />
)}
```

[npm-image]: https://img.shields.io/npm/v/@phantomstudios/ft-react-components.svg?style=flat-square&logo=react
[npm-url]: https://npmjs.org/package/@phantomstudios/ft-react-components
[npm-downloads-image]: https://img.shields.io/npm/dm/@phantomstudios/ft-react-components.svg
[npm-downloads-url]: https://npmcharts.com/compare/@phantomstudios/ft-react-components?minimal=true
[ci-image]: https://github.com/phantomstudios/ft-react-components/workflows/test/badge.svg
[ci-url]: https://github.com/phantomstudios/ft-react-components/actions