The lib is only compatible with PostCSS 7(Read more below). And it is required that you have tailwind set up first.
You will need:
- tailwindcss@latest
- postcss@^8.4.14
- autoprefixer@10.4.5
- postcss-flexbugs-fixes@^5.0.2
- postcss-import@^14.1.0
- postcss-preset-env@^7.4.4
- postcss-combine-media-query@1.0.1
Locked
autoprefixer@10.4.5andpostcss-preset-env@7.4.4because of this issue. When they fix the issue that causes the warnings we can uninstallautoprefixerand updatepostcss-preset-envto latest.
It is recommended that you read and understand all of these before starting, they are tailwind's core concepts:
- Preflight
- Optimizing for Production
- Utility-First
- Responsive Design
- Hover, Focus, & Other States
- Adding Base Styles
- Extracting Components
- Adding New Utilities
- Functions & Directives
- Go to Tailwind.css getting started, and follow the instructions.
- Here are the npm install commands to installs the correct/expected versions:
if your project has postcss-combine-duplicated-selectors and/or postcss-nested installed, make sure to remove it before running the commands bellow
npm i autoprefixer@10.4.5npm i tailwindcss@latest postcss@latest postcss-import@latest postcss-preset-env@7.4.4 @perimetre/ui@latest- Proceed the tailwind setup as usual. Following that instruction page.
-
Edit the
tailwind.config.jsfile, adding the followingpreset:module.exports = { + content: [ + './src/pages/**/*.{js,ts,jsx,tsx}', + './src/components/**/*.{js,ts,jsx,tsx}', + './node_modules/@perimetre/ui/**/*.{js,ts,jsx,tsx}', + '!./node_modules/@perimetre/ui/**/storybookMappers.tsx' // ignore the storybookMappers.tsx inside @perimetre/ui because that should only be used by the ui package itself + ], theme: { extend: {} }, plugins: [], + // eslint-disable-next-line @typescript-eslint/no-var-requires + presets: [require('@perimetre/ui').defaultPreset] }; -
Edit the
postcss.config.jsfile, adding the followingpreset:module.exports = { plugins: { + 'postcss-import': {}, // Import must come before tailwind + 'postcss-nesting': {}, tailwindcss: {}, + // Other plugins come after tailwind and before postcss-purgecss + autoprefixer + 'postcss-combine-media-query': {}, // Media query must come before duplicated-selectors + 'postcss-flexbugs-fixes': {}, // postcss-flexbugs-fixes is required to use with nextjs + // postcss-preset-env is required to use with nextjs, and it already uses autoprefixer link for details: https://nextjs.org/docs/advanced-features/customizing-postcss-config#customizing-plugins + 'postcss-preset-env': { + autoprefixer: { + flexbox: 'no-2009' + }, + stage: 3, + features: { + 'custom-properties': false, // disable nesting and let tailwindcss/nesting handle it for you instead + 'nesting-rules': false + } + } - autoprefixer: {} } };
This is not required but heavily recommended, refer to
Importing the component files individually and/or not having the font.css filesfor more information.
- As you can see in the ../src/index.css file you'll have to import later. It already applies the base
pui-font-familyclass. So you need to import the font before that file. - It is not required but you might want to read this tweet: https://twitter.com/leeerob/status/1345554815158546432.
- Make sure you have the font files, they are not included with the lib.
- Create a
font.cssfile just like this one: ../src/styles/font.css. - If you followed the tailwind setup you probably have
globals.cssfile that has all the tailwind imports, imported in a_app.tsxfile - Open the same
globals.cssfile. - Import your
font.cssin the first line of yourglobal.cssfile. Example: ../src/styles/globals.css+ @import './font.css'; + /* https://tailwindcss.com/docs/using-with-preprocessors#build-time-imports */ @import 'tailwindcss/base'; @import 'tailwindcss/components'; @import 'tailwindcss/utilities';
- You can see in your
font.cssthat it expects the font file to be served at${domain}/fonts/${...multiple font files...}. So you need to add the font files on your current app yourself. This is an example on next.js, and another one on storybook., in both cases the "public" folder is statically served. Next.js does it automatically, where on storybook we have a-ssetting declaring the public folder to be statically served. - You can change the path to the font files, you just have to statically serve them, just make sure to update everywhere accordingly.
-
If you followed the tailwind setup you probably have
globals.cssfile that has all the tailwind imports, imported in a_app.tsxfile -
Open the same
globals.cssfile. -
Add
@import '@perimetre/ui/dist/postcss/index.css';right after the last tailwind import. Like so:@import './font.css'; /* https://tailwindcss.com/docs/using-with-preprocessors#build-time-imports */ @import 'tailwindcss/base'; @import 'tailwindcss/components'; @import 'tailwindcss/utilities'; - + + @import '@perimetre/ui/dist/postcss/index.css'; +
-
You should now be able to use the
.puiclasses and the components that use those classes.
This is not required if you already have it setup in the method explained above
- You can import the component files individually in your
globals.cssby importing them in the correct order like this:/* https://tailwindcss.com/docs/using-with-preprocessors#build-time-imports */ @import 'tailwindcss/base'; + @import '@perimetre/ui/dist/postcss/components/base.css'; @import 'tailwindcss/components'; + @import '@perimetre/ui/dist/postcss/components/index.css'; @import 'tailwindcss/utilities';
- You should now be able to use the
.puiclasses and the components that use those classes.
Importing react components is as easy as doing:
import { Dropdown } from '@perimetre/ui';
<Dropdown />;After installing the lib you can find two files at ./node_modules/@perimetre/ui/dist:
index.cssindex.min.css
Which then you can use in your project however you want, by either importing it on App.js/ts, serving it on static, or hosting the desired css file and using it as a <link> tag.