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

@sajari-ui/core

v3.1.1

Published

Sajari UI Component Library

Downloads

416

Readme

Sajari UI

This is a work in progress of the Sajari UI component library, based on Tailwind.

The approach

We're going to build components using Tailwind but rather than expose the className prop on all components and let it be a free-for-all, we're going to go for an approach similar to styled system using style props but rather than m={4} we have to use the full className so purgeCSS works correctly so our version of that would be margin="m-4". We're also using TypeScript so all classNames have to be in their own type - e.g. a Margin type would contain all possible margin classNames, including any variants.

Adding it to a project

1. Install the packages

To install the production package run yarn add @sajari-ui/core.

To develop locally, you can link the package using yarn:

  • Run yarn link in this folder (/packages/sajari-ui). This will link all local installs to this package.
  • In the consuming project run yarn link "@sajari-ui/core" and then yarn add @sajari-ui/core@link:0.1.0 to install the package.

Peer dependencies

  • yarn add -D postcss postcss-clean postcss-cli autoprefixer
  • yarn add tailwindcss

2. Create stylesheet

Create the base stylesheet. Later this will be included in the package.

/* purgecss start ignore */
@tailwind base;

@tailwind components;
/* purgecss end ignore */

@tailwind utilities;

3. Setup PostCSS

Create a postcss config with this as the content:

const { tailwindConfig } = require('@sajari-ui/core');

tailwindConfig.purge = {
  mode: 'all',
  content: ['./src/**/*.tsx', '../../node_modules/@sajari-ui/core/dist/*.js'],
};

module.exports = {
  plugins: [
    require('tailwindcss')(tailwindConfig),
    require('autoprefixer'),
    require('postcss-clean')({
      level: 2, // Merge duplicated declarations
    }),
  ],
};

🚨 NOTE: The tailwindConfig.purge.content paths need to be updated to suit your application.

🚨 NOTE: The CSS will be purged for production builds only. To enable it always, you can set tailwindConfig.purge.enabled to true. Unfortunately we have to set mode to all to allow for the classNames in our package to stay in the CSS.

4. Add scripts to package.json for build

Preact CLI and Next come with PostCSS already setup so all you need to do is import the CSS file in your app and it'll get processed via PostCSS. Sadly Create React App isn't so awesome so you need to create an npm script to do it.

Before:

"scripts": {
  "start": "react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test",
  "eject": "react-scripts eject"
}

After:

"scripts": {
  "build:css": "postcss src/styles/index.css -o src/index.css",
  "watch:css": "postcss -w src/styles/index.css -o src/index.css",
  "prestart": "npm run prebuild",
  "prebuild": "npm run build:css",
  "start": "run-p watch:css start:react",
  "start:react": "react-scripts start",
  "build": "react-scripts build",
  "test": "react-scripts test",
  "eject": "react-scripts eject"
}

🚨 NOTE: You need to adjust the paths to the CSS file depending on your application. You'll also need to import the processed CSS - /src/index.css in the example above in your app.

Example

Props & Prop Categories

The types created can be used in props as-is or as categorized sets.

  • CommonProps - display, margin, padding
  • LayoutProps - display, width, height, minWidth, minHeight, maxWidth, maxHeight
  • PositionProps - position, zIndex, offset (top, right, bottom, left)
  • FlexProps - flexDirection, flexWrap, alignItems, alignContent, justifyContent, order
  • FlexItemProps - alignSelf, flex, flexGrow, flexShrink

The index.ts file in /src/types also exports the relevant keys for the categories. These are useful for mapping the props to classNames.

Generating ClassNames

When consuming the prop categories, you can use two helpers to generate the classNames:

  • mapClassNames (/src/utils/styles/map-classnames.ts) - The arguments are props and keys. Keys needs to contain the keys for the props you wish to convert to classNames.
  • filterObject (/src/utils/object/filter.ts) - Filter out keys from props - useful for doing {...rest} type stuff where you don't want the props to bleed to the DOM element.

Creating types

We've created a build script to build the TypeScript types based on the Tailwind config. In /scripts/types there are two files:

  • types.js - An array of objects containing all the types we want to build.
  • build.js - The actual build script to parse the config and create the types.

If you need to add a missing type or update them, you can run yarn build:types. The types are committed to source control so this shouldn't need to be run often - only when a change is made to the Tailwind config or we upgrade Tailwind and they add features.

TODO

  • [ ] Better docs
  • [x] Create remaining Types that are missing
  • [ ] Create more prop categories that are missing
  • [ ] Decide which prop categories apply to all components (e.g. FlexItemProps)