@procore/labs-banners
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

Banners

A handy component you can insert into a form or inline on a page to communicate results of an application action which was performed with some status (e.g. success, warnings, failure or just information and others).

Installation

yarn add @procore/labs-banners

Dependencies

@procore/core-react and react are listed as external peer dependencies. The package will not bundle the code, and requires the app client to provide it as a dependency. The external peer dep is to assure React Context is consistent in a client's React tree, the child consumers can reference the correct parent provider. If the package uses latest features or bug fixes and a new minimum version of core-react is required, it should be considered a breaking change as the peer dependency version must be met.

Usage

import { I18nProvider } from '@procore/core-react';
import { Banners } from '@procore/labs-banners';

Motivation

A large amount of components need to interact with users and provide feedback messages for users actions. Main reason of Banners component is to prevent code duplication and create common and consistent interface to handle storing and displaying important informations for users.

Key points:

  • Straightforward API
const INFO_BANNER = 'INFO_BANNER';

showBanner({
  id: INFO_BANNER,
  msg: 'Some important information',
  variant: 'success',
});
  • Easy configuration
const ACTION_BANNER = 'ACTION_BANNER';

showBanner({
  id: ACTION_BANNER,
  msg: 'Some important information',
  variant: 'success',
  action: {
    title: 'Button title',
    handler: (banner) => console.log('button clicked', banner),
  },
  dismissible: true,
  afterClose: (banner) => console.log('button clicked', banner),
});
  • Delete programmatically by ID
const INFO_BANNER = 'INFO_BANNER';

hideBanner(INFO_BANNER);
  • Id uniqueness handling (we remove previous banner with the same id)
// state - [{id: 'INFO_BANNER', msg: 'Some important information', variant: 'success'}]
const INFO_BANNER = 'INFO_BANNER';

showBanner({
  id: INFO_BANNER,
  msg: 'Another important information',
  variant: 'success',
});

// state - [{id: 'INFO_BANNER', msg: 'Another important information', variant: 'success'}]

Using built-in BannersContext

  • Banners.Provider: Component that supply store for banners.
  • Banners: Component that consumes values from the nearest context (Banners.Provider) and renders to the tree.
  • useBanners: A function wrapper around React.useContext(BannersContext).
() => {
  const Inner = () => {
    const { showBanner, hideBanner, hideAllBanners } = useBanners();

    return (
      <FlexList marginBottom="xl">
        <Button
          variant="secondary"
          onClick={() =>
            showBanner({
              id: '2',
              variant: 'success',
              msg: 'Success banner description',
            })
          }
        >
          Show Success Banner
        </Button>
        <Button variant="secondary" onClick={() => hideBanner('2')}>
          Hide Success Banner
        </Button>
        <Button variant="secondary" onClick={() => hideAllBanners()}>
          Hide All Banners
        </Button>
      </FlexList>
    );
  };

  const bannersShownByDefault: Banner[] = [
    {
      id: '1',
      msg: 'Info banner description',
      variant: 'info',
      dismissible: false,
    },
  ];

  return (
    <I18nProvider>
      <Banners.Provider initialValue={bannersShownByDefault}>
        <Inner />
        <Banners />
      </Banners.Provider>
    </I18nProvider>
  );
};

Using own State

  • Banners.Bar: Component that consumes and renders Banners.
() => {
  const Inner = () => {
    const bannersShownByDefault: Banner[] = [
      {
        id: "1",
        msg: "Info banner description",
        variant: "info",
        dismissible: false,
      },
    ];

    const [banners, setBanners] = React.useState<Banner[]>(
      bannersShownByDefault
    );

    const hideBanner = (id: Banner["id"]) => {
      setBanners((prevBanners) =>
        prevBanners.filter((banner) => banner.id !== id)
      );
    };

    const showBanner = (newBanner: Banner) => {
      const isBannerExists = banners.find(
        (banner) => banner.id === newBanner.id
      );
      if (!isBannerExists) {
        setBanners((prevBanners) => [newBanner, ...prevBanners]);
      }
    };

    const hideAllBanners = () => setBanners([]);

    return (
      <>
        <FlexList marginBottom="xl">
          <Button
            variant="secondary"
            onClick={() =>
              showBanner({
                id: "2",
                variant: "success",
                msg: "Success banner description",
              })
            }
          >
            Show Success Banner
          </Button>
          <Button variant="secondary" onClick={() => hideBanner("2")}>
            Hide Success Banner
          </Button>
          <Button variant="secondary" onClick={() => hideAllBanners()}>
            Hide All Banners
          </Button>
        </FlexList>
        <Banners.Bar banners={banners} hideBanner={hideBanner} />
      </>
    );
  };
  return (
    <I18nProvider>
      <Inner />
    </I18nProvider>
  );
};

Readme

Keywords

none

Package Sidebar

Install

npm i @procore/labs-banners

Weekly Downloads

4

Version

3.0.0

License

SEE LICENSE IN LICENSE

Unpacked Size

101 kB

Total Files

38

Last publish

Collaborators

  • yzhou2024
  • alyelashram_procore
  • melch-procore
  • peterknif
  • moaz-ashraf
  • attachi
  • a.elbadawei
  • hyogman
  • dmitri_wm
  • stephanie.brereton
  • procore-oss-user
  • stevenkang3
  • max.helmetag
  • codyrobertsprocore
  • miguel.garcia-procore
  • magdyyx
  • atoaima
  • mustafa-abdelrahman
  • elewando-procore
  • ahmed.ghorab
  • lnspatz914
  • richard.bunn
  • omar.wagdy
  • mona.khairbek
  • mbartlett413
  • cody_schindler_procore
  • yoasyo25
  • ritchlee
  • andersontr15
  • steven.hinkle
  • jamie-dugan-procore
  • hgouhierprocore
  • denzylbalram
  • sarah.freitas
  • alan.bresani
  • amyprocore
  • yoyis3000
  • elijah.procore
  • mike-arndt-procore
  • jnhoang1
  • pam-whisenhunt
  • shradha.khard
  • david-christensen-procore
  • javio-procore
  • chance.eakin.procore
  • gideon-procore
  • ihor.diachenko_procore
  • justinmwatts
  • tedyang
  • jyang-procore
  • pwhisenhunt-procore
  • fairchild
  • rodayna.ehab
  • neil1023
  • scottstern
  • brian.smith1
  • g2mitchell
  • dlameter-procore
  • kylepietz
  • abhijit-procore
  • lhuang325
  • jake-pitkin
  • erikthoreson
  • simona.iancu
  • decha-sanson
  • aberkowitz
  • asamay
  • mustafa-u-abdelrahman
  • rajatmenhdiratta
  • jacksonleach-procore
  • pmfrawley
  • phunguyen-pcor
  • tatsiana.clifton
  • deiab
  • srichaitanya.peddinti
  • kenny.foisy
  • matheusprocore
  • jgreene_procore
  • hectorthiele
  • etokarev
  • daniel.ferreira-contractor
  • dmccraw-procore
  • cyrille.bai
  • greg.sparks
  • fabiomelo513
  • phil.custer
  • bbreyel921
  • amir-iskander
  • neil.mckeeman
  • nickprocore
  • lzhou888
  • davidshure
  • stevenliprocore
  • ramysaid2
  • refaiepcn
  • jgentes
  • faraz.hanif
  • mostafaeltazy
  • agamaleldin
  • andrew.isaac
  • saranahal2
  • rodrigo.dejuana
  • kellen.stewart
  • bill-wagner
  • ezrasimeloff
  • jeffgiaquinto
  • gturkadze
  • sean.spearman.procore
  • kylemartinez-procore
  • roobo-romeski
  • andres-mendez-procore
  • gaurav.sharma.procore
  • tracy.otto
  • sarah.heredia
  • victorbendeck-pc
  • cbathgate
  • davidkangpro
  • kyle.liu
  • amin.jaipuri
  • grafffffff
  • mishaelowoyemi
  • evan.cerwonka.procore
  • ilya.dryha-contractor
  • varomir
  • yogevfine1
  • timofeee
  • matt.harris0223
  • winson.chu
  • andersonbispoprocore
  • scorgiat-procore
  • ladavarga
  • procore_halzy
  • enyaga
  • willpankonien
  • sateesh-kadiyala-procore
  • chris.berber
  • txin1
  • epalinprocore
  • mehrdad-panahandeh
  • tyler.wasden.procore
  • jeremy.lund
  • dineshkumar.jayak
  • ryanfuentesprocore
  • stajics
  • brocktillotsonprocore
  • kyle.williams
  • dtorres-procore
  • noor.ali
  • ari-procore
  • alanprocore
  • jl4ever
  • james.lawson
  • ajaykumar-procore
  • dennis.heckman
  • tara.chambers
  • lalovar-procore
  • james.cleary
  • chadryder
  • devin.cunningham.procore
  • abhijit.patwardhan
  • lydiahara
  • sherylnapigkit
  • changprocore
  • apcarroll_procore
  • andy.mayer
  • bob.laskowski
  • vinaya-procore
  • kahliholmes
  • andrew.wheeler
  • leandro-proc
  • yadhu.prakash
  • jason-kaye
  • jesse.olsen
  • patrick.lardin
  • brad.urani
  • allenanle.procore
  • brookyboy009
  • uddhavjoglekar
  • dancingshell
  • rysmithprocore
  • robbiegprocore
  • jadamsss
  • jeremy.bouzigard
  • timdoherty
  • b.bookout
  • jalyng
  • htael
  • dev-account-admin
  • sseanwang
  • bhargavrnd
  • farismmk
  • dannyporrello
  • danny.ou
  • messanjah
  • eyvettesou
  • jgee67
  • cagmz
  • mariah_delaney
  • lukenispel
  • kimhin267
  • juliana.hernandez
  • judy-lu-pc
  • procore-it-support
  • andrewburke-pc
  • jkleintech
  • rachel.arkebauer
  • procore-npm-bot
  • james.dabbs-procore
  • laurenbrandsteinprocore
  • scottbieser-procore
  • zach.mckenzie.procore
  • shayonj_procore
  • heplayskeys
  • mike.south
  • thomasoboyle
  • dischorde
  • derek-carter-procore
  • dlgasser
  • cfprocore
  • evan.waits
  • jeremy-marcus
  • jmejia-fsl
  • ersgonzalo
  • stephan-procore
  • aleclarsenprocore
  • yihai.zweifel
  • jay-rajan
  • jacky-lei
  • peter.jin