@ng-native/tailwind 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -3
- package/config.cjs +48 -0
- package/flatten.cjs +1 -1
- package/package.json +6 -6
- package/preset.cjs +3 -132
- package/tailwind-3-preset.cjs +186 -0
- package/web-preset.cjs +17 -0
- package/web-preset.d.cts +10 -0
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# @ng-native/tailwind
|
|
2
2
|
|
|
3
|
-
A Tailwind CSS preset for Angular Native, for Tailwind 4 on native and
|
|
4
|
-
|
|
3
|
+
A Tailwind CSS preset for Angular Native, for Tailwind 4 and Tailwind 3, on native and on the
|
|
4
|
+
web: `<view class="flex-1 bg-blue-500 p-4">` works because
|
|
5
5
|
[`@ng-native/fabric`](https://github.com/ng-native/ng-native/blob/main/packages/fabric) already has
|
|
6
6
|
a real cascade, and `class` already matches against it.
|
|
7
7
|
|
|
@@ -44,7 +44,7 @@ mount(rootTag, App, fabric, { globalStyles: tailwind });
|
|
|
44
44
|
Import `theme.css` and `utilities.css`, not the plain `tailwindcss` entry point - that also pulls
|
|
45
45
|
in preflight, a browser reset that means nothing on a phone.
|
|
46
46
|
|
|
47
|
-
## Tailwind 3
|
|
47
|
+
## Tailwind 3
|
|
48
48
|
|
|
49
49
|
```sh
|
|
50
50
|
npm install @ng-native/tailwind @ng-native/metro tailwindcss@3
|
|
@@ -79,12 +79,16 @@ presets: [
|
|
|
79
79
|
],
|
|
80
80
|
```
|
|
81
81
|
|
|
82
|
+
On the web, a Tailwind 3 build uses `web-preset.cjs` in place of `preset.cjs`. See
|
|
83
|
+
[Tailwind 3 on the web](https://ng-native.com/packages/web#with-tailwind-3).
|
|
84
|
+
|
|
82
85
|
## What's in the package
|
|
83
86
|
|
|
84
87
|
- `./native.css` - the preset: platform variants, safe-area and hairline utilities, and
|
|
85
88
|
touch-appropriate `hover:`/`focus-visible:` meanings.
|
|
86
89
|
- `./web.css` - the same preset's web entry point, for `@ng-native/web`.
|
|
87
90
|
- `./preset.cjs` - the native preset for Tailwind 3, used from `tailwind.config.js`.
|
|
91
|
+
- `./web-preset.cjs` - the web preset for Tailwind 3, for `@ng-native/web`.
|
|
88
92
|
- `./config.cjs` - `withTailwind`, the Metro config step that runs the app's Tailwind CLI and
|
|
89
93
|
flattens its output for the CSS compiler.
|
|
90
94
|
|
package/config.cjs
CHANGED
|
@@ -98,6 +98,47 @@ function generate(css, output, input) {
|
|
|
98
98
|
|
|
99
99
|
const warnedSettled = new Set();
|
|
100
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Where a selector's `{` is on a line: -1 when the line has none, and null when a `;` or `}` ends
|
|
103
|
+
* something first, so no selector is being written. One quoted or escaped, as an arbitrary
|
|
104
|
+
* variant's `[data-x=";"]` quotes one, ends nothing.
|
|
105
|
+
*/
|
|
106
|
+
function openingBrace(line) {
|
|
107
|
+
let quote = null;
|
|
108
|
+
for (let i = 0; i < line.length; i++) {
|
|
109
|
+
const char = line[i];
|
|
110
|
+
if (char === '\\') i++;
|
|
111
|
+
else if (quote) quote = char === quote ? null : quote;
|
|
112
|
+
else if (char === '"' || char === "'") quote = char;
|
|
113
|
+
else if (char === '{') return i;
|
|
114
|
+
else if (char === ';' || char === '}') return null;
|
|
115
|
+
}
|
|
116
|
+
return -1;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* The selector of the rule that starts on line `index`, as the app writes it: through to its `{`,
|
|
121
|
+
* which a list of selectors can put lines further on, with CSS's escapes undone, so `.md\:grid`
|
|
122
|
+
* reads `.md:grid` and `.\32 xl\:grid` reads `.2xl:grid`. Undefined when no rule starts there,
|
|
123
|
+
* which leaves the line to be named.
|
|
124
|
+
*/
|
|
125
|
+
function selectorAt(lines, index) {
|
|
126
|
+
let text = '';
|
|
127
|
+
for (let at = index; at < lines.length; at++) {
|
|
128
|
+
const brace = openingBrace(lines[at]);
|
|
129
|
+
if (brace === null) return undefined;
|
|
130
|
+
text += ` ${brace === -1 ? lines[at] : lines[at].slice(0, brace)}`;
|
|
131
|
+
if (brace !== -1) break;
|
|
132
|
+
}
|
|
133
|
+
const selector = text
|
|
134
|
+
.replace(/\\([\da-f]{1,6})\s?|\\(.)/gi, (_, hex, char) =>
|
|
135
|
+
hex ? String.fromCodePoint(parseInt(hex, 16)) : char,
|
|
136
|
+
)
|
|
137
|
+
.replace(/\s+/g, ' ')
|
|
138
|
+
.trim();
|
|
139
|
+
return selector || undefined;
|
|
140
|
+
}
|
|
141
|
+
|
|
101
142
|
/**
|
|
102
143
|
* Tailwind's CSS as a module exporting the compiled stylesheet.
|
|
103
144
|
*
|
|
@@ -120,8 +161,15 @@ function compileSheetModule(css, context = 'tailwind', paths) {
|
|
|
120
161
|
`it on device, so it does not follow where the app sets it.`,
|
|
121
162
|
);
|
|
122
163
|
}
|
|
164
|
+
// A refusal names the rule's selector, which is what to search the app for, rather than a line
|
|
165
|
+
// of this generated sheet, which moves as classes are added.
|
|
166
|
+
const lines = flat.split('\n');
|
|
123
167
|
const sheet = compileCss(flat, context, {
|
|
124
168
|
onUnsupported: (message) => dropped.push(message),
|
|
169
|
+
locate: (line) => {
|
|
170
|
+
const selector = selectorAt(lines, line - 1);
|
|
171
|
+
return selector && `${selector} (Tailwind)`;
|
|
172
|
+
},
|
|
125
173
|
});
|
|
126
174
|
for (const message of dropped) {
|
|
127
175
|
// A leftover custom property was already substituted; anything else is a utility the app used
|
package/flatten.cjs
CHANGED
|
@@ -379,7 +379,7 @@ function dropPseudoElementRules(css) {
|
|
|
379
379
|
return css.replace(/([^{}]+)\{([^{}]*)\}/g, (whole, selectors, body) => {
|
|
380
380
|
if (!selectors.includes('::')) return whole;
|
|
381
381
|
const all = selectorList(selectors).map((one) => one.trim());
|
|
382
|
-
const kept = all.filter((one) => one &&
|
|
382
|
+
const kept = all.filter((one) => one && !/::(?!placeholder\b)/.test(one));
|
|
383
383
|
// Tailwind 3's `::backdrop { --tw-...: ... }`, the reset again for a box native never draws: no
|
|
384
384
|
// author wrote it, so it goes without a word.
|
|
385
385
|
if (!kept.length && /^\s*(--tw-[\w-]+\s*:[^;{}]*;?\s*)*$/.test(body)) return '';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ng-native/tailwind",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Tailwind CSS preset for Angular Native: Tailwind 4
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "Tailwind CSS preset for Angular Native: Tailwind 4 and Tailwind 3, on native and on the web.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"angular",
|
|
7
7
|
"react-native",
|
|
@@ -17,8 +17,8 @@
|
|
|
17
17
|
"type": "commonjs",
|
|
18
18
|
"main": "flatten.cjs",
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"
|
|
21
|
-
"
|
|
20
|
+
"@ng-native/metro": "0.3.0",
|
|
21
|
+
"lightningcss": "^1.30.2"
|
|
22
22
|
},
|
|
23
23
|
"nx": {
|
|
24
24
|
"tags": [
|
|
@@ -32,9 +32,9 @@
|
|
|
32
32
|
"url": "https://github.com/ng-native/ng-native/issues"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
|
+
"@ng-native/metro": "0.3.0",
|
|
35
36
|
"@tailwindcss/cli": "^4.3.3",
|
|
36
|
-
"tailwindcss": "^3.4.1 || ^4.3.3"
|
|
37
|
-
"@ng-native/metro": "0.2.0"
|
|
37
|
+
"tailwindcss": "^3.4.1 || ^4.3.3"
|
|
38
38
|
},
|
|
39
39
|
"peerDependenciesMeta": {
|
|
40
40
|
"@tailwindcss/cli": {
|
package/preset.cjs
CHANGED
|
@@ -7,137 +7,8 @@
|
|
|
7
7
|
* content: ['./src/**\/*.{ts,html}'],
|
|
8
8
|
* };
|
|
9
9
|
*
|
|
10
|
-
*
|
|
11
|
-
* which is what an app using it alone needs. Beside another preset that also brings the defaults,
|
|
12
|
-
* the later one's defaults override the earlier one's theme, so an app lists this one after its own
|
|
13
|
-
* preset as `{ ...require('@ng-native/tailwind/preset.cjs'), presets: [] }`.
|
|
14
|
-
*
|
|
15
|
-
* The same vocabulary as `native.css`, which is the Tailwind 4 preset, in the JavaScript form
|
|
16
|
-
* Tailwind 3 takes: read the two side by side, and change them together. Every choice is explained
|
|
17
|
-
* there and in `shared.css`; this file only says how each one is spelt here.
|
|
18
|
-
*
|
|
19
|
-
* The plugin is a plain function rather than `require('tailwindcss/plugin')(...)`, which Tailwind 3
|
|
20
|
-
* accepts, so this file resolves nothing from the app.
|
|
10
|
+
* `web-preset.cjs` is the counterpart for a browser, and `tailwind-3-preset.cjs` builds both.
|
|
21
11
|
*/
|
|
12
|
+
const { tailwind3Preset } = require('./tailwind-3-preset.cjs');
|
|
22
13
|
|
|
23
|
-
|
|
24
|
-
const EDGES = { t: 'top', r: 'right', b: 'bottom', l: 'left' };
|
|
25
|
-
|
|
26
|
-
/** `var(--safe-area-inset-top, 0px)`: the inset `<safe-area-provider>` sets on the root. */
|
|
27
|
-
const inset = (edge) => `var(--safe-area-inset-${edge}, 0px)`;
|
|
28
|
-
|
|
29
|
-
const HAIRLINE = 'var(--hairline, 1px)';
|
|
30
|
-
|
|
31
|
-
/** The first family a `fontFamily` theme value names, in any of the forms Tailwind 3 takes. */
|
|
32
|
-
const firstFamily = (value) =>
|
|
33
|
-
String([value].flat(2)[0] ?? '')
|
|
34
|
-
.split(',')[0]
|
|
35
|
-
.trim();
|
|
36
|
-
|
|
37
|
-
function nativePreset({ addBase, addUtilities, addVariant, config, matchUtilities, theme }) {
|
|
38
|
-
// `hover:` is the pressed state, or a real hover where there is a pointer.
|
|
39
|
-
addVariant('hover', ['&:active', '&[data-hover]']);
|
|
40
|
-
addVariant('press', '&:active');
|
|
41
|
-
addVariant('hovered', '&[data-hover]');
|
|
42
|
-
// `focus-visible:` is `focus:`, and both follow the state attribute.
|
|
43
|
-
addVariant('focus-visible', ['&:focus', '&[data-focus]']);
|
|
44
|
-
addVariant('focus', ['&:focus', '&[data-focus]']);
|
|
45
|
-
addVariant('disabled', ['&[data-disabled]', '&:disabled']);
|
|
46
|
-
|
|
47
|
-
// Tailwind 3 builds `group-*:` and `peer-*:` from the pseudo-class itself rather than from the
|
|
48
|
-
// variants above, so each state is spelt again for an ancestor `.group` and an earlier `.peer`.
|
|
49
|
-
const states = {
|
|
50
|
-
hover: [':active', '[data-hover]'],
|
|
51
|
-
focus: [':focus', '[data-focus]'],
|
|
52
|
-
'focus-visible': [':focus', '[data-focus]'],
|
|
53
|
-
disabled: ['[data-disabled]', ':disabled'],
|
|
54
|
-
};
|
|
55
|
-
for (const [state, selectors] of Object.entries(states)) {
|
|
56
|
-
addVariant(
|
|
57
|
-
`group-${state}`,
|
|
58
|
-
selectors.map((s) => `:merge(.group)${s} &`),
|
|
59
|
-
);
|
|
60
|
-
addVariant(
|
|
61
|
-
`peer-${state}`,
|
|
62
|
-
selectors.map((s) => `:merge(.peer)${s} ~ &`),
|
|
63
|
-
);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
// The platform, from a class on the root.
|
|
67
|
-
addVariant('ios', '.platform-ios &');
|
|
68
|
-
addVariant('android', '.platform-android &');
|
|
69
|
-
addVariant('web', '.platform-web &');
|
|
70
|
-
addVariant('native', ['.platform-ios &', '.platform-android &']);
|
|
71
|
-
// Tailwind 3 puts an app's `prefix` on every class in these, `.tw-platform-ios &` and `.tw-dark &`,
|
|
72
|
-
// and nothing sets those on the root. The prefix is recorded for `flattenTailwind`, which takes
|
|
73
|
-
// it back off the root classes.
|
|
74
|
-
if (config('prefix')) {
|
|
75
|
-
addBase({ ':root': { '--ng-native-tailwind-prefix': JSON.stringify(config('prefix')) } });
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
const safe = {
|
|
79
|
-
'.p-safe': Object.fromEntries(
|
|
80
|
-
Object.values(EDGES).map((edge) => [`padding-${edge}`, inset(edge)]),
|
|
81
|
-
),
|
|
82
|
-
'.px-safe': { 'padding-right': inset('right'), 'padding-left': inset('left') },
|
|
83
|
-
'.py-safe': { 'padding-top': inset('top'), 'padding-bottom': inset('bottom') },
|
|
84
|
-
};
|
|
85
|
-
for (const [letter, edge] of Object.entries(EDGES)) {
|
|
86
|
-
safe[`.p${letter}-safe`] = { [`padding-${edge}`]: inset(edge) };
|
|
87
|
-
safe[`.m${letter}-safe`] = { [`margin-${edge}`]: inset(edge) };
|
|
88
|
-
}
|
|
89
|
-
addUtilities(safe);
|
|
90
|
-
|
|
91
|
-
// The inset plus a step off the spacing scale, and whichever of the two is larger.
|
|
92
|
-
// Lengths only: a bare number after the inset is no length, and a browser drops it.
|
|
93
|
-
const spacing = { values: theme('spacing'), type: ['length', 'percentage'] };
|
|
94
|
-
for (const [letter, edge] of [
|
|
95
|
-
['t', 'top'],
|
|
96
|
-
['b', 'bottom'],
|
|
97
|
-
]) {
|
|
98
|
-
matchUtilities(
|
|
99
|
-
{
|
|
100
|
-
[`p${letter}-safe`]: (step) => ({ [`padding-${edge}`]: `calc(${inset(edge)} + ${step})` }),
|
|
101
|
-
[`m${letter}-safe`]: (step) => ({ [`margin-${edge}`]: `calc(${inset(edge)} + ${step})` }),
|
|
102
|
-
[`min-p${letter}-safe`]: (step) => ({
|
|
103
|
-
[`padding-${edge}`]: `max(${inset(edge)}, ${step})`,
|
|
104
|
-
}),
|
|
105
|
-
},
|
|
106
|
-
spacing,
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const hairlines = {
|
|
111
|
-
'.h-hairline': { height: HAIRLINE },
|
|
112
|
-
'.w-hairline': { width: HAIRLINE },
|
|
113
|
-
'.border-hairline': { 'border-style': 'solid', 'border-width': HAIRLINE },
|
|
114
|
-
};
|
|
115
|
-
for (const [letter, edge] of Object.entries(EDGES)) {
|
|
116
|
-
hairlines[`.border-${letter}-hairline`] = {
|
|
117
|
-
'border-style': 'solid',
|
|
118
|
-
'border-width': '0',
|
|
119
|
-
[`border-${edge}-width`]: HAIRLINE,
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
addUtilities(hairlines);
|
|
123
|
-
|
|
124
|
-
// `font-mono`, when it is still Tailwind's own stack, whose first family, `ui-monospace`, is on
|
|
125
|
-
// neither platform: `Courier New` on both, upgraded to the font each platform uses for code. An
|
|
126
|
-
// app that names its own monospace font keeps it. Utilities rather than base rules, so an app's
|
|
127
|
-
// `important: true` makes them important too, and the platform rules still win.
|
|
128
|
-
if (firstFamily(theme('fontFamily.mono')) === 'ui-monospace') {
|
|
129
|
-
addUtilities({
|
|
130
|
-
'.font-mono': { 'font-family': 'Courier New' },
|
|
131
|
-
'.platform-ios .font-mono': { 'font-family': 'Menlo' },
|
|
132
|
-
'.platform-android .font-mono': { 'font-family': 'monospace' },
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
module.exports = {
|
|
138
|
-
// A class seeded from the OS, so an app can disagree with it.
|
|
139
|
-
darkMode: ['variant', '.dark &'],
|
|
140
|
-
// A browser reset in terms of `html` and `::before`, none of which means anything on a phone.
|
|
141
|
-
corePlugins: { preflight: false },
|
|
142
|
-
plugins: [nativePreset],
|
|
143
|
-
};
|
|
14
|
+
module.exports = tailwind3Preset('native');
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The Tailwind 3 presets, `preset.cjs` for native and `web-preset.cjs` for a browser, both built by
|
|
3
|
+
* `tailwind3Preset(platform)`.
|
|
4
|
+
*
|
|
5
|
+
* There is no `presets` key here, so Tailwind 3 adds its default configuration beneath this preset,
|
|
6
|
+
* which is what an app using it alone needs. Beside another preset that also brings the defaults,
|
|
7
|
+
* the later one's defaults override the earlier one's theme, so an app lists this one after its own
|
|
8
|
+
* preset as `{ ...require('@ng-native/tailwind/preset.cjs'), presets: [] }`.
|
|
9
|
+
*
|
|
10
|
+
* The same vocabulary as `native.css` and `web.css`, which are the Tailwind 4 presets, in the
|
|
11
|
+
* JavaScript form Tailwind 3 takes: read them side by side, and change them together. Every choice
|
|
12
|
+
* is explained there and in `shared.css`; this file only says how each one is spelt here.
|
|
13
|
+
*
|
|
14
|
+
* The plugin is a plain function rather than `require('tailwindcss/plugin')(...)`, which Tailwind 3
|
|
15
|
+
* accepts, so this file resolves nothing from the app.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
/** The four safe-area edges, keyed by the letter a spacing utility uses for them. */
|
|
19
|
+
const EDGES = { t: 'top', r: 'right', b: 'bottom', l: 'left' };
|
|
20
|
+
|
|
21
|
+
/** `var(--safe-area-inset-top, 0px)`: the inset `<safe-area-provider>` sets on the root. */
|
|
22
|
+
const inset = (edge) => `var(--safe-area-inset-${edge}, 0px)`;
|
|
23
|
+
|
|
24
|
+
const HAIRLINE = 'var(--hairline, 1px)';
|
|
25
|
+
|
|
26
|
+
/** The first family a `fontFamily` theme value names, in any of the forms Tailwind 3 takes. */
|
|
27
|
+
const firstFamily = (value) =>
|
|
28
|
+
String([value].flat(2)[0] ?? '')
|
|
29
|
+
.split(',')[0]
|
|
30
|
+
.trim();
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* The states each variant reads. On native, `hover:` is the pressed state, or a real hover where
|
|
34
|
+
* there is a pointer, and `focus-visible:` is `focus:`. A browser has `:hover` and `:focus-visible`
|
|
35
|
+
* for real, so there they lead and the others stay, as in `web.css`.
|
|
36
|
+
*/
|
|
37
|
+
const STATES = {
|
|
38
|
+
native: {
|
|
39
|
+
hover: [':active', '[data-hover]'],
|
|
40
|
+
hovered: ['[data-hover]'],
|
|
41
|
+
focus: [':focus', '[data-focus]'],
|
|
42
|
+
'focus-visible': [':focus', '[data-focus]'],
|
|
43
|
+
disabled: ['[data-disabled]', ':disabled'],
|
|
44
|
+
},
|
|
45
|
+
web: {
|
|
46
|
+
hover: [':hover', ':active', '[data-hover]'],
|
|
47
|
+
hovered: [':hover', '[data-hover]'],
|
|
48
|
+
focus: [':focus', '[data-focus]'],
|
|
49
|
+
'focus-visible': [':focus-visible', '[data-focus]'],
|
|
50
|
+
disabled: ['[data-disabled]', ':disabled'],
|
|
51
|
+
},
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* A variant that matches beneath a class on the root, such as `dark` or `platform-ios`.
|
|
56
|
+
*
|
|
57
|
+
* Tailwind 3 puts an app's `prefix` on every class in a variant's selector, `.tw-dark &`, and the
|
|
58
|
+
* root wears `dark`. On native the prefix is recorded for `flattenTailwind`, which takes it back
|
|
59
|
+
* off. A browser has no such step, so there the class is matched as an attribute, which the prefix
|
|
60
|
+
* leaves alone and which is as specific as a class.
|
|
61
|
+
*/
|
|
62
|
+
const beneath = (platform, name) => (platform === 'web' ? `[class~="${name}"] &` : `.${name} &`);
|
|
63
|
+
|
|
64
|
+
/** `web.css`'s root tokens: the browser's own safe-area insets, and a hairline per density. */
|
|
65
|
+
const WEB_BASE = {
|
|
66
|
+
':root': Object.fromEntries(
|
|
67
|
+
Object.values(EDGES).map((edge) => [
|
|
68
|
+
`--safe-area-inset-${edge}`,
|
|
69
|
+
`env(safe-area-inset-${edge}, 0px)`,
|
|
70
|
+
]),
|
|
71
|
+
),
|
|
72
|
+
'@media (min-resolution: 2dppx)': { ':root': { '--hairline': '0.5px' } },
|
|
73
|
+
'@media (min-resolution: 3dppx)': { ':root': { '--hairline': '0.33px' } },
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
/** @param {'native' | 'web'} platform */
|
|
77
|
+
function presetPlugin(
|
|
78
|
+
platform,
|
|
79
|
+
{ addBase, addUtilities, addVariant, config, matchUtilities, theme },
|
|
80
|
+
) {
|
|
81
|
+
const states = STATES[platform];
|
|
82
|
+
const on = (selectors) => selectors.map((s) => `&${s}`);
|
|
83
|
+
addVariant('hover', on(states.hover));
|
|
84
|
+
addVariant('press', '&:active');
|
|
85
|
+
addVariant('hovered', on(states.hovered));
|
|
86
|
+
addVariant('focus-visible', on(states['focus-visible']));
|
|
87
|
+
addVariant('focus', on(states.focus));
|
|
88
|
+
addVariant('disabled', on(states.disabled));
|
|
89
|
+
|
|
90
|
+
// Tailwind 3 builds `group-*:` and `peer-*:` from the pseudo-class itself rather than from the
|
|
91
|
+
// variants above, so each state is spelt again for an ancestor `.group` and an earlier `.peer`.
|
|
92
|
+
for (const state of ['hover', 'focus', 'focus-visible', 'disabled']) {
|
|
93
|
+
addVariant(
|
|
94
|
+
`group-${state}`,
|
|
95
|
+
states[state].map((s) => `:merge(.group)${s} &`),
|
|
96
|
+
);
|
|
97
|
+
addVariant(
|
|
98
|
+
`peer-${state}`,
|
|
99
|
+
states[state].map((s) => `:merge(.peer)${s} ~ &`),
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// The platform, from a class on the root.
|
|
104
|
+
addVariant('ios', beneath(platform, 'platform-ios'));
|
|
105
|
+
addVariant('android', beneath(platform, 'platform-android'));
|
|
106
|
+
addVariant('web', beneath(platform, 'platform-web'));
|
|
107
|
+
addVariant('native', [beneath(platform, 'platform-ios'), beneath(platform, 'platform-android')]);
|
|
108
|
+
if (platform === 'web') addBase(WEB_BASE);
|
|
109
|
+
else if (config('prefix')) {
|
|
110
|
+
// Read by `flattenTailwind`, which takes the prefix back off the root classes.
|
|
111
|
+
addBase({ ':root': { '--ng-native-tailwind-prefix': JSON.stringify(config('prefix')) } });
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
const safe = {
|
|
115
|
+
'.p-safe': Object.fromEntries(
|
|
116
|
+
Object.values(EDGES).map((edge) => [`padding-${edge}`, inset(edge)]),
|
|
117
|
+
),
|
|
118
|
+
'.px-safe': { 'padding-right': inset('right'), 'padding-left': inset('left') },
|
|
119
|
+
'.py-safe': { 'padding-top': inset('top'), 'padding-bottom': inset('bottom') },
|
|
120
|
+
};
|
|
121
|
+
for (const [letter, edge] of Object.entries(EDGES)) {
|
|
122
|
+
safe[`.p${letter}-safe`] = { [`padding-${edge}`]: inset(edge) };
|
|
123
|
+
safe[`.m${letter}-safe`] = { [`margin-${edge}`]: inset(edge) };
|
|
124
|
+
}
|
|
125
|
+
addUtilities(safe);
|
|
126
|
+
|
|
127
|
+
// The inset plus a step off the spacing scale, and whichever of the two is larger.
|
|
128
|
+
// Lengths only: a bare number after the inset is no length, and a browser drops it.
|
|
129
|
+
const spacing = { values: theme('spacing'), type: ['length', 'percentage'] };
|
|
130
|
+
for (const [letter, edge] of [
|
|
131
|
+
['t', 'top'],
|
|
132
|
+
['b', 'bottom'],
|
|
133
|
+
]) {
|
|
134
|
+
matchUtilities(
|
|
135
|
+
{
|
|
136
|
+
[`p${letter}-safe`]: (step) => ({ [`padding-${edge}`]: `calc(${inset(edge)} + ${step})` }),
|
|
137
|
+
[`m${letter}-safe`]: (step) => ({ [`margin-${edge}`]: `calc(${inset(edge)} + ${step})` }),
|
|
138
|
+
[`min-p${letter}-safe`]: (step) => ({
|
|
139
|
+
[`padding-${edge}`]: `max(${inset(edge)}, ${step})`,
|
|
140
|
+
}),
|
|
141
|
+
},
|
|
142
|
+
spacing,
|
|
143
|
+
);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
const hairlines = {
|
|
147
|
+
'.h-hairline': { height: HAIRLINE },
|
|
148
|
+
'.w-hairline': { width: HAIRLINE },
|
|
149
|
+
'.border-hairline': { 'border-style': 'solid', 'border-width': HAIRLINE },
|
|
150
|
+
};
|
|
151
|
+
for (const [letter, edge] of Object.entries(EDGES)) {
|
|
152
|
+
hairlines[`.border-${letter}-hairline`] = {
|
|
153
|
+
'border-style': 'solid',
|
|
154
|
+
'border-width': '0',
|
|
155
|
+
[`border-${edge}-width`]: HAIRLINE,
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
addUtilities(hairlines);
|
|
159
|
+
|
|
160
|
+
// `font-mono`, when it is still Tailwind's own stack, whose first family, `ui-monospace`, is on
|
|
161
|
+
// neither platform: `Courier New` on both, upgraded to the font each platform uses for code. An
|
|
162
|
+
// app that names its own monospace font keeps it. Utilities rather than base rules, so an app's
|
|
163
|
+
// `important: true` makes them important too, and the platform rules still win. A browser has
|
|
164
|
+
// the whole stack, so the web keeps it.
|
|
165
|
+
if (platform === 'native' && firstFamily(theme('fontFamily.mono')) === 'ui-monospace') {
|
|
166
|
+
addUtilities({
|
|
167
|
+
'.font-mono': { 'font-family': 'Courier New' },
|
|
168
|
+
'.platform-ios .font-mono': { 'font-family': 'Menlo' },
|
|
169
|
+
'.platform-android .font-mono': { 'font-family': 'monospace' },
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** @param {'native' | 'web'} platform */
|
|
175
|
+
function tailwind3Preset(platform) {
|
|
176
|
+
return {
|
|
177
|
+
// A class seeded from the OS, so an app can disagree with it.
|
|
178
|
+
darkMode: ['variant', beneath(platform, 'dark')],
|
|
179
|
+
// A browser reset in terms of `html` and `::before`. None of it means anything on a phone, and
|
|
180
|
+
// in a browser the host's `reset.css` is the reset.
|
|
181
|
+
corePlugins: { preflight: false },
|
|
182
|
+
plugins: [(api) => presetPlugin(platform, api)],
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
module.exports = { tailwind3Preset };
|
package/web-preset.cjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The web preset, for Tailwind 3 in a browser through `@ng-native/web`.
|
|
3
|
+
*
|
|
4
|
+
* // tailwind.config.js, for the web build
|
|
5
|
+
* module.exports = {
|
|
6
|
+
* presets: [require('@ng-native/tailwind/web-preset.cjs')],
|
|
7
|
+
* content: ['./src/**\/*.{ts,html}'],
|
|
8
|
+
* };
|
|
9
|
+
*
|
|
10
|
+
* The Tailwind 3 counterpart of `web.css`, as `preset.cjs` is of `native.css`: `hover:` and
|
|
11
|
+
* `focus-visible:` are the browser's own, the safe area and the hairline come from the browser,
|
|
12
|
+
* and `font-mono` keeps Tailwind's stack. The platform and dark variants match the classes
|
|
13
|
+
* `mount` keeps on the root under an app's `prefix` too. `tailwind-3-preset.cjs` builds both.
|
|
14
|
+
*/
|
|
15
|
+
const { tailwind3Preset } = require('./tailwind-3-preset.cjs');
|
|
16
|
+
|
|
17
|
+
module.exports = tailwind3Preset('web');
|
package/web-preset.d.cts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The web preset for Tailwind 3, for a `tailwind.config.ts`. Spelt out rather than typed as
|
|
3
|
+
* `Config` from `tailwindcss`, so it holds whichever major of Tailwind the import resolves to.
|
|
4
|
+
*/
|
|
5
|
+
declare const preset: {
|
|
6
|
+
darkMode: ['variant', string];
|
|
7
|
+
corePlugins: { preflight: false };
|
|
8
|
+
plugins: ((api: unknown) => void)[];
|
|
9
|
+
};
|
|
10
|
+
export = preset;
|