14.2.1 • Published 21 days ago

@ithaka/pharos v14.2.1

Weekly downloads
-
License
MIT
Repository
github
Last release
21 days ago

Pharos Web Components

Web Components are a set of web platform APIs that allow you to create new custom, reusable, encapsulated HTML tags to use in web pages and web apps.

Pharos provides a web component library for building products consistent with the JSTOR brand.

If you're using Vue, you can use these components in the same manner specified, without any additional steps!

Installation

$ npm install @ithaka/pharos

Registering components

  1. To allow multiple versions of Pharos to exist on a page, this package only exports component classes for you to register on the custom element registry in your application. To register components, you can use the registerComponents utility to define all the Pharos components your application uses, with a consistent scoping prefix:
import { PharosAlert, PharosButton, PharosIcon } from '@ithaka/pharos';
import registerComponents from '@ithaka/pharos/lib/utils/registerComponents';

registerComponents('homepage', [PharosAlert, PharosButton, PharosIcon]);

Note: When using module federation, you may only share Pharos successfully by using the registerComponents utility. This avoids registering elements with the same underlying class instance, which would result in an error.

To manually register a component, import the classes you wish to use in your application's entrypoint and define the custom element with a tag name in the form of {app/bundle}-pharos-{component} and a trivial subclass that extends the Pharos class wrapped in the PharosComponentMixin:

import { PharosAlert } from '@ithaka/pharos';
import PharosComponentMixin from '@ithaka/pharos/lib/utils/mixins/pharos-component';

customElements.define('homepage-pharos-alert', class extends PharosComponentMixin(PharosAlert) {});

Note: If you register a name that already exists the browser will throw an error about the duplicate.

  1. Internally, Pharos components that are composed of other Pharos components scope their registries to their shadow root to avoid duplicate registrations. Because the Scoped Custom Element Registries proposal is not yet finalized, you need to apply a polyfill to use our components.

  2. Every component sets a custom data attribute data-pharos-component on itself with its class name (such as PharosAlert) to allow you to query any instance of that component regardless of its defined tag. Components also use this attribute to locate slotted Pharos children. Bundlers minify these class names by default for production builds. To ensure components work as expected, update your configurations like so:

Webpack (Terser):

optimization: {
  minimizer: [
    new TerserPlugin({
      terserOptions: {
        keep_classnames: /^Pharos/,
        keep_fnames: /^Pharos/,
      }
    }),
  ],
}

Vite (ESBuild):

export default defineConfig({
  esbuild: {
    keepNames: true,
  },
});

Using web components

After installing Pharos and registering the components, you can render them in any of your templates:

<pharos-tooltip>
  <button>Hover here!</button>
  <span slot="content">I am a tooltip</span>
</pharos-tooltip>

See the web component Storybook for details on component-specific syntax.

Using Pharos components in React

React is a JavaScript library used to build encapsulated components that manage their own state. React doesn't currently play perfectly with native web components, so if you're developing a React application you need to use the wrapper components provided in the lib/react-components/ directory of the package.

To use the React components, first register the components using the registerComponents utility. Then, in your app's entry file, use the PharosContext context provider to indicate the scoping prefix you registered them under:

import { PharosContext } from '@ithaka/pharos/lib/utils/PharosContext';

const context = { prefix: 'homepage' };

Then, render it in your JSX:

<PharosContext.Provider value={context}>...app code</PharosContext.Provider>

To use a component, first import it:

import { PharosTooltip } from '@ithaka/pharos/lib/react-components';

Then, render the component in your JSX:

<PharosTooltip content={'I am a tooltip'}>Hover here!</PharosTooltip>

See the React Storybook for details on component-specific syntax.

Styling components

Pharos components utilize CSS variables for their styling. To style components across your app, import the variables like so in your styles entrypoint:

/* index.css */

@import '@ithaka/pharos/lib/styles/variables.css';

/* More global styles */

Using Pharos design tokens

Components in Pharos are styled using design tokens, a tech agnostic way to store design decisions such as typography, color, and spacing so that Pharos can be shared across platforms. You can use these tokens to help style your own components and pages to ensure the brand is properly expressed to users. The token files are located in the lib/styles/ directory of the package.

To use the tokens as SASS variables, import them like so in the file where you want to use them:

/* example-page.scss */

@import '@ithaka/pharos/lib/styles/_variables';

/* More styling for the example page */

To use the tokens as ESM modules in your JavaScript, import them like so to ensure unused tokens are tree-shaken:

/* example-page.js */

import { PharosColorTextBase } from '@ithaka/pharos/lib/styles/variables';

Typography and mixins

Pharos uses Ivar a serif primarily for headlines and GT America a sans serif for body copy. The font-face stylesheet and base typography styles are located in the lib/styles/ directory of the package. Import them, along with the design tokens, in your app's styles entrypoint file:

/* index.scss */

@import '@ithaka/pharos/lib/styles/fonts.css';
@import '@ithaka/pharos/lib/styles/variables.css';
@import '@ithaka/pharos/lib/styles/typography';

/* More global styles and imports */

Pharos also provides SASS mixins which are reusable styles shared across multiple components. Import them like so in the file where you want to use them:

/* example-page.scss */

@use '@ithaka/pharos/lib/styles/pharos';

.some-text {
  @include pharos.font-base;
}

The base typography styles and mixins utilize SASS modules which replaces @import with @use to make CSS, variables, mixins, and functions from another stylesheet accessible in the current stylesheet. If your project uses Webpack for bundling, you will need to make sure sass-loader is set to use dart-sass instead of node-sass as Node Sass is deprecated and does not support SASS modules. If using sass-loader >= 9.0.0 no change is required as sass is set as the default. Otherwise, update your Webpack config as so:

/* webpack.config.js */

{
  loader: 'sass-loader',
  options: {
    implementation: require('sass'),
  }
}

You can access all Pharos variables, mixins, and functions from a single pharos.scss entrypoint file. Import it in the file where you want to use its modules:

/* example-page.scss */

@use '@ithaka/pharos/lib/styles/pharos';

.some-text {
  @include pharos.font-base;

  color: pharos.$pharos-color-text-20;
}

Additional component styles

Most components in Pharos benefit from the fully isolated styling provided by web components and CSS variables. However, some components provide slots you can populate with your own content. Content provided by application authors is rendered in the light DOM, and is not always stylable by web components.

You may need to import additional CSS files into your project's build for components whose slots expect nested content. These files are located in the lib/styles/ directory of the package. To use the CSS, import it in whichever bundle will be included on the same page as your component:

/* example-page.css */

@import '@ithaka/pharos/lib/styles/pharos-alert.css';

/* More styling for the example page */

Selectors in component stylesheets are scoped to the component's host element (<pharos-alert> in the above example).

Components that have corresponding stylesheets are:

  • pharos-alert
  • pharos-footer
  • pharos-modal

Using Pharos form elements in forms

Pharos form elements listen to the formdata event to add their values to the enclosing <form>. Safari doesn't natively fire the formdata event, so form authors need to fire a custom event in its stead.

Pharos provides a cross-browser createFormData utility function for populating forms via a custom formdata event. You should call createFormData in your form's submit event handler.

import createFormData from '@ithaka/pharos/lib/utils/createFormData.js';

const form = document.querySelector('form');
form.addEventListener('submit', (event) => createFormData(event.target));

If you submit your forms asynchronously, you can pass the result of createFormData in your request:

import createFormData from '@ithaka/pharos/lib/utils/createFormData.js';

const form = document.querySelector('form');

form.addEventListener('submit', (event) => {
  event.preventDefault();

  const formData = createFormData(event.target);

  const xhr = new XMLHttpRequest();
  xhr.open('POST', 'https://some-url.com');
  xhr.send(formData);
});

Adoption Governance Model

Take a look at the adoption governance model for a quick overview of the adoption process.

14.2.1

21 days ago

14.2.0

1 month ago

14.1.0

2 months ago

14.0.1

2 months ago

14.0.0

2 months ago

13.4.0

3 months ago

13.3.0

3 months ago

12.23.0

8 months ago

12.25.0

8 months ago

12.21.0

9 months ago

13.1.0

7 months ago

12.26.1

8 months ago

12.26.2

7 months ago

12.22.1

8 months ago

12.24.0

8 months ago

12.26.0

8 months ago

12.20.0

10 months ago

12.20.1

10 months ago

12.22.0

9 months ago

12.20.2

9 months ago

13.2.2

6 months ago

13.2.0

7 months ago

13.2.1

7 months ago

13.0.0

7 months ago

13.0.1

7 months ago

12.19.1

12 months ago

12.19.2

11 months ago

12.19.3

11 months ago

12.19.0

1 year ago

12.18.0

1 year ago

12.17.2

1 year ago

12.17.1

1 year ago

12.16.0

1 year ago

12.17.0

1 year ago

12.15.1

1 year ago

12.15.0

1 year ago

12.12.0

2 years ago

12.14.0

2 years ago

12.13.0

2 years ago

12.8.0

2 years ago

12.10.0

2 years ago

12.9.0

2 years ago

12.9.1

2 years ago

12.7.3

2 years ago

12.11.0

2 years ago

12.7.4

2 years ago

12.7.0

2 years ago

12.6.1

2 years ago

12.7.1

2 years ago

12.6.2

2 years ago

12.7.2

2 years ago

12.5.0

2 years ago

12.6.0

2 years ago

12.5.1

2 years ago

12.3.0

2 years ago

12.4.0

2 years ago

12.3.1

2 years ago

12.2.1

2 years ago

12.2.2

2 years ago

12.2.3

2 years ago

12.1.0

2 years ago

12.2.0

2 years ago

12.0.0

2 years ago

12.0.1

2 years ago

11.2.0

2 years ago

11.2.1

2 years ago

11.1.0

2 years ago

11.0.1

2 years ago

11.0.0

3 years ago

10.8.2

3 years ago

10.8.0

3 years ago

10.8.1

3 years ago

10.7.0

3 years ago

10.6.3

3 years ago

10.6.2

3 years ago

10.6.1

3 years ago

10.6.0

3 years ago

10.5.3

3 years ago

10.5.2

3 years ago

10.5.0

3 years ago

10.5.1

3 years ago

10.4.0

3 years ago

10.3.1

3 years ago

10.2.0

3 years ago

10.3.0

3 years ago

10.1.0

3 years ago

10.0.0

3 years ago

9.3.0

3 years ago

9.2.1

3 years ago

9.2.0

3 years ago

9.1.0

3 years ago

9.0.0

3 years ago