Skip to content

Commit b89b863

Browse files
authored
fix: @import (#63)
1 parent 3658a7b commit b89b863

File tree

6 files changed

+57
-25
lines changed

6 files changed

+57
-25
lines changed

README.md

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
MistCSS lets you create reusable visual components without JavaScript or TypeScript (_think about it for a second... no JS/TS needed_).
66

7-
Leverage native HTML and CSS, get type safety and auto completion. Just clean and efficient styling.
7+
Leverage native HTML and CSS, get type safety and autocomplete. Just clean and efficient styling.
88

99
<img width="1116" alt="Screenshot 2024-11-01 at 03 47 44" src="https://github.com/user-attachments/assets/74aea071-be00-4d03-b43a-e46d6282e4b5">
1010

@@ -109,6 +109,9 @@ import './mist.css'
109109

110110
Absolutely, MistCSS is pure HTML and CSS, generating only `mist.d.ts`, so there are no limitations. You can integrate any CSS framework seamlessly. Here are a few examples to get you started:
111111

112+
> [!IMPORTANT]
113+
> For the best experience, set up Tailwind IntelliSense in your editor. Refer to [Tailwind's editor setup guide](https://tailwindcss.com/docs/editor-setup).
114+
112115
#### Tailwind v3 ([@apply](https://tailwindcss.com/docs/functions-and-directives#apply))
113116

114117
```css
@@ -155,6 +158,21 @@ button {
155158

156159
CSS is more powerful than ever, before reaching for JS, explore if native CSS features can accomplish what you need.
157160

161+
### Can I write `<name>` instead of `data-<name>`?
162+
163+
No, using `<name>` would result in invalid HTML. However, this constraint is actually advantageous.
164+
165+
Firstly, it eliminates the risk of conflicts with native attributes:
166+
167+
```jsx
168+
<>
169+
<Button type="primary">Save</Button {/* Conflict with button's type="submit" */}
170+
<button data-type="primary">Save</button> {/* Safe */}
171+
</>
172+
```
173+
174+
Additionally, just by typing `data-` in your editor, autocomplete helps you clearly distinguish your custom attributes from standard tag attributes.
175+
158176
### How to write enum, boolean, string props and conditions?
159177

160178
```css
@@ -191,14 +209,14 @@ div[data-component='section']
191209
/>
192210
```
193211

194-
### How to re-use the same tag
212+
### How to re-use the same tag?
195213

196214
If you want both basic links and button-styled links, here’s how you can do:
197215

198216
```css
199217
a { /* ... */ }
200218

201-
a[data-component='button'] { /* ... */
219+
a[data-component='button'] { /* ... */ }
202220
&[data-variant='primary'] { /* ... */ }
203221
}
204222
```
@@ -215,6 +233,13 @@ a[data-component='button'] { /* ... */
215233
> [!NOTE]
216234
> `data-component` is just a naming convention. Feel free to use any attribute, like `data-style='button'` or `data-button`. It’s simply a way to differentiate between components using the same tag.
217235
236+
### How to split my code?
237+
238+
You can use CSS [@import](https://developer.mozilla.org/en-US/docs/Web/CSS/@import). For example, in your `mist.css` file:
239+
240+
```css
241+
@import './button.css'
242+
```
218243

219244
### How to build complex components?
220245

@@ -242,7 +267,7 @@ export function Card({ title, children }) {
242267
> [!TIP]
243268
> To indicate that these styles aren't meant to be used outside of `Card`, you can name them `data-p-component` (`p` for `private`) or use another naming convention.
244269

245-
### How to define CSS variables
270+
### How to define CSS variables?
246271

247272
```css
248273
:root {

package-lock.json

Lines changed: 12 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,18 @@
2424
"devDependencies": {
2525
"@tsconfig/node18": "^18.2.4",
2626
"@types/node": "^20.11.19",
27+
"@types/postcss-import": "^14.0.3",
2728
"eslint": "^8.56.0",
2829
"husky": "^9.0.11",
2930
"postcss": "^8.4.47",
3031
"postcss-cli": "^11.0.0",
31-
"postcss-import": "^16.1.0",
3232
"prettier": "^3.2.5",
3333
"tsx": "^4.19.2",
3434
"typescript": "^5.3.3",
3535
"vitest": "^1.5.0"
3636
},
3737
"dependencies": {
38+
"postcss-import": "^16.1.0",
3839
"postcss-selector-parser": "^6.1.2"
3940
}
4041
}

postcss.config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
module.exports = {
22
plugins: [
3-
require('postcss-import'),
43
require('./lib/index.js')({})
54
]
65
}

src/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import fs = require('node:fs')
22
import { type PluginCreator } from 'postcss'
33
import selectorParser = require('postcss-selector-parser');
4+
import atImport = require("postcss-import")
45
import path = require('node:path');
56
const html = require('./html')
67

@@ -104,10 +105,10 @@ function initialParsedValue(): Parsed[keyof Parsed] {
104105
}
105106
}
106107

107-
export const mistcss: PluginCreator<{}> = (_opts = {}) => {
108+
const _mistcss: PluginCreator<{}> = (_opts = {}) => {
108109
return {
109-
postcssPlugin: 'mistcss',
110-
Once(root, helper) {
110+
postcssPlugin: '_mistcss',
111+
async Once(root, helper) {
111112
// Only parse mist.css file
112113
const from = helper.result.opts.from
113114
if (from === undefined || path.basename(from) !== 'mist.css') return
@@ -147,6 +148,15 @@ export const mistcss: PluginCreator<{}> = (_opts = {}) => {
147148
}
148149
}
149150

151+
_mistcss.postcss = true
152+
153+
export const mistcss: PluginCreator<{}> = (_opts = {}) => {
154+
return {
155+
postcssPlugin: 'mistcss',
156+
plugins: [atImport(), _mistcss()]
157+
}
158+
}
159+
150160
mistcss.postcss = true
151161

152162
// Needed to make PostCSS happy

test/mist.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ declare namespace JSX {
66
interface IntrinsicElements {
77
button: Mist_button
88
}
9-
}
9+
}

0 commit comments

Comments
 (0)