|
| 1 | +# Global CSS Architecture |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Component CSS is now centralized in a global `styles.css` file to prevent duplicate CSS imports when users consume the library. |
| 6 | + |
| 7 | +## Structure |
| 8 | + |
| 9 | +``` |
| 10 | +src/ |
| 11 | +βββ styles.css # Global CSS entry point |
| 12 | +βββ index.ts # Imports styles.css once |
| 13 | +βββ components/ |
| 14 | + βββ Input/ |
| 15 | + βββ Input.new.tsx # No CSS import |
| 16 | + βββ Input.new.css # Styles imported via styles.css |
| 17 | +``` |
| 18 | + |
| 19 | +## How It Works |
| 20 | + |
| 21 | +1. **Component CSS files** (`Input.new.css`) contain the styles |
| 22 | +2. **Global styles.css** imports all component CSS files |
| 23 | +3. **Main index.ts** imports `styles.css` once |
| 24 | +4. **Users** get all CSS automatically when they import any component |
| 25 | + |
| 26 | +## Benefits |
| 27 | + |
| 28 | +β
**No duplicate CSS** - CSS is imported once at the package level |
| 29 | +β
**Automatic tree-shaking** - Bundlers can optimize unused CSS |
| 30 | +β
**Better performance** - Single CSS import instead of per-component |
| 31 | +β
**Easier maintenance** - All CSS imports in one place |
| 32 | + |
| 33 | +## Adding New Components |
| 34 | + |
| 35 | +When creating a new component with CSS: |
| 36 | + |
| 37 | +1. Create the component CSS file (e.g., `Button.new.css`) |
| 38 | +2. Add import to `src/styles.css`: |
| 39 | + ```css |
| 40 | + @import './components/Button/Button.new.css'; |
| 41 | + ``` |
| 42 | +3. **Do NOT** import CSS in the component `.tsx` file |
| 43 | + |
| 44 | +## Storybook Configuration |
| 45 | + |
| 46 | +The `.storybook/preview.mjs` imports the global CSS file: |
| 47 | + |
| 48 | +```javascript |
| 49 | +import './preview.css' |
| 50 | +import '../src/styles.css' // Global component styles |
| 51 | +``` |
| 52 | + |
| 53 | +This ensures all component styles are available in Storybook stories. |
| 54 | + |
| 55 | +## User Consumption |
| 56 | + |
| 57 | +Users automatically get all CSS when importing components: |
| 58 | + |
| 59 | +```tsx |
| 60 | +// This imports both the component AND all CSS |
| 61 | +import { InputNew } from '@equinor/eds-core-react' |
| 62 | +``` |
| 63 | + |
| 64 | +Alternatively, users can import just the CSS if needed: |
| 65 | + |
| 66 | +```tsx |
| 67 | +import '@equinor/eds-core-react/styles.css' |
| 68 | +``` |
| 69 | + |
| 70 | +## Package Configuration |
| 71 | + |
| 72 | +The `package.json` includes: |
| 73 | + |
| 74 | +- `"sideEffects": ["**/*.css"]` - Preserves CSS imports during tree-shaking |
| 75 | +- `"./styles.css": "./dist/esm/styles.css"` - Exports CSS separately if needed |
0 commit comments