Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add unocss config #131

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9,622 changes: 2,983 additions & 6,639 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,4 @@
"dependencies": {
"konsta": "file:src"
}
}
}
6 changes: 4 additions & 2 deletions package/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"import": "./svelte/konsta-svelte.js"
},
"./config": "./config.js",
"./config.js": "./config.js"
"./config.js": "./config.js",
"./configUnocss": "./configUnocss.js",
"./configUnocss.js": "./configUnocss.js"
}
}
}
3 changes: 1 addition & 2 deletions src/config/plugin-base.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
const plugin = require('tailwindcss/plugin');
const hexToRgb = require('./hex-to-rgb.js');
const iosColors = require('./ios-colors.js');
const mdColors = require('./md-colors.js');

module.exports = (userConfig = {}, konstConfig = {}) => {
module.exports = (userConfig = {}, konstConfig = {}, plugin) => {
let iosFont =
'-apple-system, SF Pro Text, SF UI Text, system-ui, Helvetica Neue, Helvetica, Arial, sans-serif';
let materialFont = 'Roboto, system-ui, Noto, Helvetica, Arial, sans-serif';
Expand Down
3 changes: 1 addition & 2 deletions src/config/plugin-colors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const plugin = require('tailwindcss/plugin');
const hexToRgb = require('./hex-to-rgb.js');
const iosColors = require('./ios-colors.js');
const mdColors = require('./md-colors.js');
Expand All @@ -20,7 +19,7 @@ const rulesForColor = (name, hex) => {
};
};

module.exports = (konstaConfig) =>
module.exports = (konstaConfig, plugin) =>
plugin(({ addUtilities }) => {
const themeColors = konstaConfig.colors;

Expand Down
4 changes: 1 addition & 3 deletions src/config/plugin-hairlines.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const plugin = require('tailwindcss/plugin');

module.exports = () =>
module.exports = (plugin) =>
plugin(({ addUtilities, theme }) => {
const themeColors = theme('colors');
const themeDurations = theme('transitionDuration');
Expand Down
4 changes: 1 addition & 3 deletions src/config/plugin-ios-material.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const plugin = require('tailwindcss/plugin');

module.exports = () =>
module.exports = (plugin) =>
plugin(({ addVariant, e }) => {
addVariant('material', ({ modifySelectors, separator }) => {
modifySelectors(({ className, selector }) => {
Expand Down
4 changes: 1 addition & 3 deletions src/config/plugin-line-clamp.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const plugin = require('tailwindcss/plugin');

module.exports = () =>
module.exports = (plugin) =>
plugin(({ addUtilities }) => {
const lineClamp = {};
Array.from({ length: 10 }).forEach((_, index) => {
Expand Down
4 changes: 1 addition & 3 deletions src/config/plugin-no-scrollbar.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const plugin = require('tailwindcss/plugin');

module.exports = () =>
module.exports = (plugin) =>
plugin(({ addUtilities }) => {
const noScrollbar = {
'.no-scrollbar': {
Expand Down
4 changes: 1 addition & 3 deletions src/config/plugin-preloader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const plugin = require('tailwindcss/plugin');

module.exports = () =>
module.exports = (plugin) =>
plugin(({ addComponents }) => {
addComponents({
'.k-ios-preloader > span': {
Expand Down
4 changes: 1 addition & 3 deletions src/config/plugin-range.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const plugin = require('tailwindcss/plugin');

module.exports = () =>
module.exports = (plugin) =>
plugin(({ addVariant, e }) => {
addVariant('range-thumb', ({ modifySelectors, separator }) => {
modifySelectors(({ className }) => {
Expand Down
4 changes: 1 addition & 3 deletions src/config/plugin-safe-areas.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const plugin = require('tailwindcss/plugin');

module.exports = () =>
module.exports = (plugin) =>
plugin(({ addUtilities, addBase, theme, e }) => {
const base = {
':root': {
Expand Down
3 changes: 1 addition & 2 deletions src/config/plugin-touch-ripple.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const plugin = require('tailwindcss/plugin');
const hexToRgb = require('./hex-to-rgb.js');

module.exports = () =>
module.exports = (plugin) =>
plugin(({ addUtilities, addBase, theme }) => {
addBase({
// prettier-ignore
Expand Down
4 changes: 1 addition & 3 deletions src/config/plugin-touch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const plugin = require('tailwindcss/plugin');

module.exports = () =>
module.exports = (plugin) =>
plugin(({ addUtilities }) => {
const touch = {};
const values = ['pan-x', 'pan-y', 'none', 'manipulation'];
Expand Down
4 changes: 1 addition & 3 deletions src/config/plugin-translucent.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const plugin = require('tailwindcss/plugin');

module.exports = () =>
module.exports = (plugin) =>
plugin(({ addUtilities }) => {
addUtilities({
'@supports (backdrop-filter: blur(20px))': {
Expand Down
142 changes: 142 additions & 0 deletions src/configUnocss.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
const pluginBase = require('./config/plugin-base.js');
const pluginColors = require('./config/plugin-colors.js');
const pluginPreloader = require('./config/plugin-preloader.js');
const pluginHairlines = require('./config/plugin-hairlines.js');
const pluginTouchRipple = require('./config/plugin-touch-ripple.js');
const pluginIosMaterial = require('./config/plugin-ios-material.js');
const pluginLineClamp = require('./config/plugin-line-clamp.js');
const pluginSafeAreas = require('./config/plugin-safe-areas.js');
const pluginTranslucent = require('./config/plugin-translucent.js');
const pluginRange = require('./config/plugin-range.js');
const pluginTouch = require('./config/plugin-touch.js');
const pluginNoScrollbar = require('./config/plugin-no-scrollbar.js');

const config = (optionsG = {}) => {
const konstaConfig = {
dark: optionsG.dark || 'class',
colors: {
primary: '#007aff',
},
};
const rules = [
// [/^text-(.*)$/, ([, c], { theme }) => {
// if (theme.colors[c])
// return { color: theme.colors[c] }
// }],
// [/^m-(\d)$/, ([, d]) => ({ margin: `${d / 4}rem` }), { layer: 'utilities' }],
// when you omit the layer, it will be `default`
// ['btn', { padding: '4px' }],
];
const shortcuts = [
// // you could still have object style
// {
// btn: 'py-2 px-4 font-semibold rounded-lg shadow-md',
// },
// // dynamic shortcuts
// [/^btn-(.*)$/, ([, c]) => `bg-${c}-400 text-${c}-100 py-2 px-4 rounded-lg`],
];
const variants = [
// // hover:
// (matcher) => {
// if (!matcher.startsWith('hover:'))
// return matcher
// return {
// // slice `hover:` prefix and passed to the next variants and rules
// matcher: matcher.slice(6),
// selector: s => `${s}:hover`,
// }
// }
];
const preflights = [
// {
// layer: 'my-layer',
// getCSS: async () => (await fetch('my-style.css')).text(),
// },
];
// const preprocess = (_matcher) => {
// // return matcher.startsWith('prefix-')
// // ? matcher.slice(7)
// // : undefined // ignore
// return undefined;
// };

const plugin = (plg) => {
// plg is a PostCSS plugin
plg({
config: (key) => konstaConfig[key],
theme: (key) => konstaConfig[key],
addBase: (res) => {
Object.entries(res).forEach(([selector, properties]) => {
if (typeof selector === 'string') {
rules.push([selector, properties]);
} else if (typeof selector === 'object') {
Object.entries(selector).forEach(
([nestedSelector, nestedProperties]) => {
rules.push([nestedSelector, nestedProperties]);
}
);
}
});
},
addUtilities: (res, options = {}) => {
Object.entries(res).forEach(([selector, properties]) => {
const layer = options.layer || 'utilities';
if (typeof selector === 'string') {
rules.push([selector, properties, { layer }]);
} else if (typeof selector === 'object') {
Object.entries(selector).forEach(
([nestedSelector, nestedProperties]) => {
rules.push([nestedSelector, nestedProperties, { layer }]);
}
);
}
});
},
addComponents: (res, options = {}) => {
Object.entries(res).forEach(([selector, properties]) => {
const layer = options.layer || 'components';
if (typeof selector === 'string') {
rules.push([selector, properties, { layer }]);
} else if (typeof selector === 'object') {
Object.entries(selector).forEach(
([nestedSelector, nestedProperties]) => {
rules.push([nestedSelector, nestedProperties, { layer }]);
}
);
}
});
},
addVariant(name, variantFn) {
variants.push([name, variantFn]);
},
});
};
pluginBase({}, konstaConfig, plugin);
pluginPreloader(plugin);
pluginIosMaterial(plugin);
pluginHairlines(plugin);
pluginTouchRipple(plugin);
pluginLineClamp(plugin);
pluginSafeAreas(plugin);
pluginTranslucent(plugin);
pluginRange(plugin);
pluginTouch(plugin);
pluginNoScrollbar(plugin);
pluginColors(konstaConfig, plugin);

return {
name: 'unocss-preset-konstaui',
extendTheme(theme) {
theme.colors = {
...konstaConfig.colors,
};
},
rules,
variants,
shortcuts,
preflights,
// preprocess,
};
};

module.exports = config;