Woolworths Atomic Design System
Welcome to the Woolworths Atomic Design System, an easy way to consume common reusable web components for use with various modern application frameworks (angular, vue, react).
Our design system is a collection of Web Components (using Stencil js by the lovely Ionic team), so they work in any major framework or with no framework at all.
QUICK START
- The repository contains stencil, stoybook and gatsby
- gatsby project has it's own
package.json
and build process -docs
sub-directory
GATSBY
- to run gasby you have to build static assets from stencil and storybook first
- after
npm install
runnpm run build:docs
thennpm run docs:start
to start working with gatsby - in
src/pages
you’ll see the mdx files, basically add stencil components as regular html, except using the object spread method{...{ attributes here }}
for attaching the attributes/props - to use react components in the mdx you’ll need to import them at the top
- each page should be wrapped in
<Layout></Layout>
- navigation can be updated from gatsby-config.js inside of the siteMetaData -> navigation object. First level is a group of nav items, then nav items are added into the items array. Second level nesting is achieved by setting the parent value to the parent path. A parent path of / will indicate top level within the group. There is a group with top and example next level item to use as reference. Active state is not currently working.
When you’re ready to deploy, just commit and push changes, docs will be auto built and deployed to s3 under the branch name used. Root domain should be http://ww-rewards-ds.s3-website-ap-southeast-2.amazonaws.com/
STORYBOOK
Integration between storybook and stencil inspired by: https://andreaswissel.com/integrating-stencil-with-storybook
Getting Started
You can learn about and contribute to the Atomic Design System in a number of ways:
Browse our online documentation
Take a look at our Design System's online documentation, found at rewards-ds.netlify.com.
Build locally to push new components or to prototype
If you are looking to either extend the Reward Design System, by adding new elements, components, or views, OR are looking to create interactive coded prototypes using the Atomic Design System, simply follow these steps:
git clone https://github.com/mothershipvc/woolworths-rewards-ds.git rewards-dscd rewards-dsgit remote rm origin
and run:
npm installnpm start
Installing Reward DS packages in your app
You can use various Atomic Design System properties and components as packages via NPM, within your Angular, Vue, React, or Static apps!
Install NPM Packages into your app's repo
npm install rewards-ds
If you only want to use a specific collection or component, install it like so:
npm install rewards-ds@core ORnpm install rewards-ds@forms OR specific components, likenpm install rewards-ds@balance-panel
Using Reward DS packages in your app
Once you've installed the relevant Atomic Design System node packages into your repo, simply do one of the following (depending on what kind of app you're building):
Add the component(s) to the dependencies
Add the component to the app dependencies in package.json:
// package.json "dependencies":
Integration with Frameworks
Angular
Using a Stencil built web component collection within an Angular CLI project is a two-step process. We need to:
- Include the
CUSTOM_ELEMENTS_SCHEMA
in the modules that use the components. - Call
defineCustomElements(window)
frommain.ts
(or some other appropriate place).
Including the Custom Elements Schema
Including the CUSTOM_ELEMENTS_SCHEMA
in the module allows the use of the web components in the HTML markup without the compiler producing errors this code should be added into the AppModule
and in every other modules that use your custom elements.
Here is an example of adding it to AppModule
:
;;; ;
The CUSTOM_ELEMENTS_SCHEMA
needs to be included in any module that uses custom elements.
Calling defineCustomElements
A component collection built with Stencil includes a main function that is used to load the components in the collection. That function is called defineCustomElements()
and it needs to be called once during the bootstrapping of your application. One convenient place to do this is in main.ts
as such:
;; ;; // Note: loader import location set using "esmLoaderPath" within the output target confg; if environment.production platformBrowserDynamic .bootstrapModuleAppModule .catchconsole.logerr;defineCustomElementswindow;
Edge and IE11 polyfills
If you want your custom elements to be able to work on older browser, you should add the applyPolyfills()
that surrond the defineCustomElements()
function.
;...applyPolyfills.then
Accessing components using ViewChild and ViewChildren
Once included, components could be referenced in your code using ViewChild
and ViewChildren
as in the following example:
; ;
React
With an application built using the create-react-app
script the easiest way to include the component library is to call defineCustomElements(window)
from the index.js
file.
Note that in this scenario applyPolyfills
is needed if you are targeting Edge or IE11.
;;;;; // test-component is the name of our made up Web Component that we have// published to npm:; ReactDOM.render, document.getElementById'root';registerServiceWorker; applyPolyfills.then;
Following the steps above will enable your web components to be used in React, however there are some additional complexities that must also be considered. https://custom-elements-everywhere.com/ describes them well.
Nuxt.js
Create a plugin, (e.g rewards.ts):
; ; Vue.config.productionTip = false;Vue.config.ignoredElements = ; // Bind the custom elements to the window objectdefineCustomElementswindow;
// nuxt.config.ts
Then, use it in any component!
<template> <div> <rds-barcode member-id="978020137962"></rds-barcode> </div></template>
Without a framework
Integrating a component built with Stencil to a project without a JavaScript framework is straight forward. If you're using a simple HTML page, you can add your component via a script tag. For example, if we published a component to npm, we could load the component through unpkg like this:
Alternatively, if you wanted to take advantage of ES Modules, you could include the components using an import statement. Note that in this scenario applyPolyfills
is needed if you are targeting Edge or IE11.
Passing object props from a non-JSX element
Setting the prop manually
;
Watching props changes
;