@scarlab-icons/js 0.0.0-stage → 3.1.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/LICENSE +21 -0
- package/README.md +83 -2
- package/dist/index.cjs +106 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +101 -0
- package/package.json +48 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alex Moore
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,84 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center"><a href="https://scarlab-icons.la-moore.ru" target="_blank" rel="noopener noreferrer"><img width="100" src="https://raw.githubusercontent.com/la-moore/scarlab-icons/master/.github/logo.svg" alt="Scarlab logo"></a></p>
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
<p align="center">
|
|
4
|
+
<a href="https://www.npmjs.com/package/@scarlab-icons/js"><img src="https://img.shields.io/npm/v/@scarlab-icons/js?color=%23cb0000" alt="npm version"></a>
|
|
5
|
+
<a href="https://www.npmjs.com/package/@scarlab-icons/js"><img src="https://img.shields.io/npm/dm/@scarlab-icons/js" alt="npm version"></a>
|
|
6
|
+
</p>
|
|
7
|
+
|
|
8
|
+
# [Scarlab Icons](http://scarlab-icons.la-moore.ru/)
|
|
9
|
+
Scarlab Icons is a collection of more than 1400 simple, beautiful and open source icons. Each icon is designed on a 24x24 grid with an emphasis on simplicity, consistency, and flexibility.
|
|
10
|
+
|
|
11
|
+
[scarlab-icons](http://scarlab-icons.la-moore.ru/)
|
|
12
|
+
|
|
13
|
+
```shell
|
|
14
|
+
npm install @scarlab-icons/js
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### Include
|
|
18
|
+
```js
|
|
19
|
+
import { replace } from '@scarlab-icons/js'
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Use
|
|
23
|
+
|
|
24
|
+
```html
|
|
25
|
+
<!-- HTML elements with data attributes -->
|
|
26
|
+
<div data-scarlab="activity" data-style="base" class="icon"></div>
|
|
27
|
+
<div data-scarlab="heart" data-style="ghost" stroke="red"></div>
|
|
28
|
+
<div data-scarlab="star" data-style="outline" data-stroke-width="3"></div>
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
// Auto-replace elements with data-scarlab attribute
|
|
33
|
+
replace({
|
|
34
|
+
stroke: 'currentColor',
|
|
35
|
+
strokeWidth: 2
|
|
36
|
+
})
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Data Attributes
|
|
40
|
+
|
|
41
|
+
- `data-scarlab` - Icon name (required)
|
|
42
|
+
- `data-style` - Icon style: `base`, `ghost`, `outline`, `solid` (optional, default: `base`)
|
|
43
|
+
- `data-stroke-width` - Stroke width for non-solid icons (optional, default: `2`)
|
|
44
|
+
|
|
45
|
+
### Custom Attributes
|
|
46
|
+
|
|
47
|
+
Any other HTML attributes will be passed through to the generated SVG element:
|
|
48
|
+
|
|
49
|
+
```html
|
|
50
|
+
<div data-scarlab="activity"
|
|
51
|
+
data-style="base"
|
|
52
|
+
class="custom-icon"
|
|
53
|
+
width="32"
|
|
54
|
+
height="32"
|
|
55
|
+
stroke="#ff6800">
|
|
56
|
+
</div>
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Accessibility
|
|
60
|
+
Icons are decorative by default and rendered with `aria-hidden="true"`. Add `aria-label` (or `aria-labelledby`) to give an icon an accessible name — `aria-hidden` is then omitted:
|
|
61
|
+
|
|
62
|
+
```html
|
|
63
|
+
<div data-scarlab="heart" aria-label="Like"></div>
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### API
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
import { getIcon, toSvg } from '@scarlab-icons/js'
|
|
70
|
+
|
|
71
|
+
const icon = getIcon('heart', 'outline') // IconNode | undefined, style defaults to 'base'
|
|
72
|
+
const svg = toSvg(icon, { width: 32, strokeWidth: 3, class: 'my-icon' })
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
`toSvg` converts camelCase names to kebab-case (`strokeWidth` → `stroke-width`), escapes values and skips `null`, `undefined` and `false`.
|
|
76
|
+
|
|
77
|
+
## Figma
|
|
78
|
+
Scarlab Icons is available as a [Figma component library](https://www.figma.com/community/file/1051907827478622063/Scarab-Icons). To use the components, log in to your Figma account and duplicate the file to your drafts.
|
|
79
|
+
|
|
80
|
+
Or you can use Scarlab Icons as a [Figma plugin](https://www.figma.com/community/plugin/1053846082213772972/Scarlab-Icons)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
Scarlab Icons is licensed under the [MIT License](https://github.com/la-moore/scarlab-icons/blob/master/LICENSE)..
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var icons = require('@scarlab-icons/icons');
|
|
4
|
+
|
|
5
|
+
// SVG-атрибуты, которые пишутся в camelCase и не должны превращаться в kebab-case
|
|
6
|
+
const CAMEL_CASE_ATTRS = new Set([
|
|
7
|
+
'viewBox',
|
|
8
|
+
'preserveAspectRatio',
|
|
9
|
+
'gradientTransform',
|
|
10
|
+
'gradientUnits',
|
|
11
|
+
'patternUnits',
|
|
12
|
+
'clipPathUnits',
|
|
13
|
+
]);
|
|
14
|
+
function escapeAttr(value) {
|
|
15
|
+
return value
|
|
16
|
+
.replace(/&/g, '&')
|
|
17
|
+
.replace(/"/g, '"')
|
|
18
|
+
.replace(/</g, '<')
|
|
19
|
+
.replace(/>/g, '>');
|
|
20
|
+
}
|
|
21
|
+
function escapeText(value) {
|
|
22
|
+
return value
|
|
23
|
+
.replace(/&/g, '&')
|
|
24
|
+
.replace(/</g, '<')
|
|
25
|
+
.replace(/>/g, '>');
|
|
26
|
+
}
|
|
27
|
+
/** strokeWidth → stroke-width; viewBox остаётся как есть */
|
|
28
|
+
function normalizeAttrName(name) {
|
|
29
|
+
if (CAMEL_CASE_ATTRS.has(name))
|
|
30
|
+
return name;
|
|
31
|
+
return name.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);
|
|
32
|
+
}
|
|
33
|
+
const VALID_ATTR_NAME = /^[a-zA-Z_:][a-zA-Z0-9_:.-]*$/;
|
|
34
|
+
function attrsToString(attrs) {
|
|
35
|
+
return Object.entries(attrs)
|
|
36
|
+
.filter(([key, value]) => value !== null && value !== undefined && value !== false && VALID_ATTR_NAME.test(key))
|
|
37
|
+
.map(([key, value]) => (value === true ? key : `${key}="${escapeAttr(String(value))}"`))
|
|
38
|
+
.join(' ');
|
|
39
|
+
}
|
|
40
|
+
function renderNode(node) {
|
|
41
|
+
if (node.type === 'text') {
|
|
42
|
+
return escapeText(node.value ?? '');
|
|
43
|
+
}
|
|
44
|
+
const attrs = attrsToString(node.attributes ?? {});
|
|
45
|
+
const children = (node.children ?? []).map(renderNode).join('');
|
|
46
|
+
return `<${node.name}${attrs ? ' ' + attrs : ''}>${children}</${node.name}>`;
|
|
47
|
+
}
|
|
48
|
+
function toSvg(icon, attrs = {}) {
|
|
49
|
+
const normalized = {};
|
|
50
|
+
for (const [key, value] of Object.entries(attrs)) {
|
|
51
|
+
normalized[normalizeAttrName(key)] = value;
|
|
52
|
+
}
|
|
53
|
+
const className = [`scarlab scarlab-${icon.name}`, normalized.class]
|
|
54
|
+
.filter(Boolean)
|
|
55
|
+
.join(' ');
|
|
56
|
+
// декоративная иконка скрыта от скринридеров, пока ей не дали доступное имя
|
|
57
|
+
const labelled = normalized['aria-label'] || normalized['aria-labelledby'];
|
|
58
|
+
const combinedAttrs = {
|
|
59
|
+
...icon.svg.attributes,
|
|
60
|
+
'aria-hidden': labelled ? undefined : 'true',
|
|
61
|
+
...normalized,
|
|
62
|
+
class: className,
|
|
63
|
+
};
|
|
64
|
+
const children = (icon.svg.children ?? []).map(renderNode).join('');
|
|
65
|
+
return `<svg ${attrsToString(combinedAttrs)}>${children}</svg>`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const DEFAULT_STYLE = 'base';
|
|
69
|
+
// Служебные атрибуты, которые не переносятся на итоговый <svg>
|
|
70
|
+
const SERVICE_ATTRS = new Set(['data-scarlab', 'data-style']);
|
|
71
|
+
// data-* атрибуты, которые маппятся на атрибуты SVG
|
|
72
|
+
const DATA_ATTR_ALIASES = {
|
|
73
|
+
'data-stroke-width': 'stroke-width',
|
|
74
|
+
};
|
|
75
|
+
function getIcon(name, style = DEFAULT_STYLE) {
|
|
76
|
+
return icons.icons.find((icon) => icon.name === name && icon.style === style);
|
|
77
|
+
}
|
|
78
|
+
function replaceByAttribute(params = {}, root = document) {
|
|
79
|
+
const elements = root.querySelectorAll('[data-scarlab]');
|
|
80
|
+
for (const element of elements) {
|
|
81
|
+
const iconName = element.dataset.scarlab;
|
|
82
|
+
const iconStyle = element.dataset.style || DEFAULT_STYLE;
|
|
83
|
+
const icon = iconName ? getIcon(iconName, iconStyle) : undefined;
|
|
84
|
+
if (!icon)
|
|
85
|
+
continue;
|
|
86
|
+
const attrs = {};
|
|
87
|
+
for (const attribute of Array.from(element.attributes)) {
|
|
88
|
+
const name = attribute.name;
|
|
89
|
+
if (SERVICE_ATTRS.has(name))
|
|
90
|
+
continue;
|
|
91
|
+
attrs[DATA_ATTR_ALIASES[name] ?? name] = attribute.value;
|
|
92
|
+
}
|
|
93
|
+
element.outerHTML = toSvg(icon, {
|
|
94
|
+
...params,
|
|
95
|
+
...attrs,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
function replace(params = {}, root) {
|
|
100
|
+
replaceByAttribute(params, root);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
exports.getIcon = getIcon;
|
|
104
|
+
exports.replace = replace;
|
|
105
|
+
exports.replaceByAttribute = replaceByAttribute;
|
|
106
|
+
exports.toSvg = toSvg;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { IconNode } from '@scarlab-icons/icons';
|
|
2
|
+
|
|
3
|
+
type Attrs = Record<string, string | number | boolean | null | undefined>;
|
|
4
|
+
declare function toSvg(icon: IconNode, attrs?: Attrs): string;
|
|
5
|
+
|
|
6
|
+
declare function getIcon(name: string, style?: string): IconNode | undefined;
|
|
7
|
+
declare function replaceByAttribute(params?: Attrs, root?: ParentNode): void;
|
|
8
|
+
declare function replace(params?: Attrs, root?: ParentNode): void;
|
|
9
|
+
|
|
10
|
+
export { getIcon, replace, replaceByAttribute, toSvg };
|
|
11
|
+
export type { Attrs };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { icons } from '@scarlab-icons/icons';
|
|
2
|
+
|
|
3
|
+
// SVG-атрибуты, которые пишутся в camelCase и не должны превращаться в kebab-case
|
|
4
|
+
const CAMEL_CASE_ATTRS = new Set([
|
|
5
|
+
'viewBox',
|
|
6
|
+
'preserveAspectRatio',
|
|
7
|
+
'gradientTransform',
|
|
8
|
+
'gradientUnits',
|
|
9
|
+
'patternUnits',
|
|
10
|
+
'clipPathUnits',
|
|
11
|
+
]);
|
|
12
|
+
function escapeAttr(value) {
|
|
13
|
+
return value
|
|
14
|
+
.replace(/&/g, '&')
|
|
15
|
+
.replace(/"/g, '"')
|
|
16
|
+
.replace(/</g, '<')
|
|
17
|
+
.replace(/>/g, '>');
|
|
18
|
+
}
|
|
19
|
+
function escapeText(value) {
|
|
20
|
+
return value
|
|
21
|
+
.replace(/&/g, '&')
|
|
22
|
+
.replace(/</g, '<')
|
|
23
|
+
.replace(/>/g, '>');
|
|
24
|
+
}
|
|
25
|
+
/** strokeWidth → stroke-width; viewBox остаётся как есть */
|
|
26
|
+
function normalizeAttrName(name) {
|
|
27
|
+
if (CAMEL_CASE_ATTRS.has(name))
|
|
28
|
+
return name;
|
|
29
|
+
return name.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);
|
|
30
|
+
}
|
|
31
|
+
const VALID_ATTR_NAME = /^[a-zA-Z_:][a-zA-Z0-9_:.-]*$/;
|
|
32
|
+
function attrsToString(attrs) {
|
|
33
|
+
return Object.entries(attrs)
|
|
34
|
+
.filter(([key, value]) => value !== null && value !== undefined && value !== false && VALID_ATTR_NAME.test(key))
|
|
35
|
+
.map(([key, value]) => (value === true ? key : `${key}="${escapeAttr(String(value))}"`))
|
|
36
|
+
.join(' ');
|
|
37
|
+
}
|
|
38
|
+
function renderNode(node) {
|
|
39
|
+
if (node.type === 'text') {
|
|
40
|
+
return escapeText(node.value ?? '');
|
|
41
|
+
}
|
|
42
|
+
const attrs = attrsToString(node.attributes ?? {});
|
|
43
|
+
const children = (node.children ?? []).map(renderNode).join('');
|
|
44
|
+
return `<${node.name}${attrs ? ' ' + attrs : ''}>${children}</${node.name}>`;
|
|
45
|
+
}
|
|
46
|
+
function toSvg(icon, attrs = {}) {
|
|
47
|
+
const normalized = {};
|
|
48
|
+
for (const [key, value] of Object.entries(attrs)) {
|
|
49
|
+
normalized[normalizeAttrName(key)] = value;
|
|
50
|
+
}
|
|
51
|
+
const className = [`scarlab scarlab-${icon.name}`, normalized.class]
|
|
52
|
+
.filter(Boolean)
|
|
53
|
+
.join(' ');
|
|
54
|
+
// декоративная иконка скрыта от скринридеров, пока ей не дали доступное имя
|
|
55
|
+
const labelled = normalized['aria-label'] || normalized['aria-labelledby'];
|
|
56
|
+
const combinedAttrs = {
|
|
57
|
+
...icon.svg.attributes,
|
|
58
|
+
'aria-hidden': labelled ? undefined : 'true',
|
|
59
|
+
...normalized,
|
|
60
|
+
class: className,
|
|
61
|
+
};
|
|
62
|
+
const children = (icon.svg.children ?? []).map(renderNode).join('');
|
|
63
|
+
return `<svg ${attrsToString(combinedAttrs)}>${children}</svg>`;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const DEFAULT_STYLE = 'base';
|
|
67
|
+
// Служебные атрибуты, которые не переносятся на итоговый <svg>
|
|
68
|
+
const SERVICE_ATTRS = new Set(['data-scarlab', 'data-style']);
|
|
69
|
+
// data-* атрибуты, которые маппятся на атрибуты SVG
|
|
70
|
+
const DATA_ATTR_ALIASES = {
|
|
71
|
+
'data-stroke-width': 'stroke-width',
|
|
72
|
+
};
|
|
73
|
+
function getIcon(name, style = DEFAULT_STYLE) {
|
|
74
|
+
return icons.find((icon) => icon.name === name && icon.style === style);
|
|
75
|
+
}
|
|
76
|
+
function replaceByAttribute(params = {}, root = document) {
|
|
77
|
+
const elements = root.querySelectorAll('[data-scarlab]');
|
|
78
|
+
for (const element of elements) {
|
|
79
|
+
const iconName = element.dataset.scarlab;
|
|
80
|
+
const iconStyle = element.dataset.style || DEFAULT_STYLE;
|
|
81
|
+
const icon = iconName ? getIcon(iconName, iconStyle) : undefined;
|
|
82
|
+
if (!icon)
|
|
83
|
+
continue;
|
|
84
|
+
const attrs = {};
|
|
85
|
+
for (const attribute of Array.from(element.attributes)) {
|
|
86
|
+
const name = attribute.name;
|
|
87
|
+
if (SERVICE_ATTRS.has(name))
|
|
88
|
+
continue;
|
|
89
|
+
attrs[DATA_ATTR_ALIASES[name] ?? name] = attribute.value;
|
|
90
|
+
}
|
|
91
|
+
element.outerHTML = toSvg(icon, {
|
|
92
|
+
...params,
|
|
93
|
+
...attrs,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
function replace(params = {}, root) {
|
|
98
|
+
replaceByAttribute(params, root);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export { getIcon, replace, replaceByAttribute, toSvg };
|
package/package.json
CHANGED
|
@@ -1,6 +1,49 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@scarlab-icons/js",
|
|
3
|
+
"version": "3.1.0",
|
|
4
|
+
"description": "A set of free MIT-licensed SVG icons.",
|
|
5
|
+
"repository": "https://github.com/la-moore/scarlab-icons",
|
|
6
|
+
"homepage": "https://scarlab-icons.la-moore.ru",
|
|
7
|
+
"author": "moorlex",
|
|
8
|
+
"license": "MIT",
|
|
9
|
+
"keywords": [
|
|
10
|
+
"icons",
|
|
11
|
+
"svg",
|
|
12
|
+
"js"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"main": "./dist/index.cjs",
|
|
17
|
+
"module": "./dist/index.js",
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"dist/",
|
|
21
|
+
"LICENSE",
|
|
22
|
+
"README.md"
|
|
23
|
+
],
|
|
24
|
+
"exports": {
|
|
25
|
+
".": {
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
27
|
+
"import": "./dist/index.js",
|
|
28
|
+
"require": "./dist/index.cjs"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@scarlab-icons/icons": "^3.1.0"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"prebuild": "node ../../scripts/clean.mjs dist",
|
|
36
|
+
"build": "rollup -c"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@rollup/plugin-commonjs": "^28.0.3",
|
|
40
|
+
"@rollup/plugin-node-resolve": "^16.0.0",
|
|
41
|
+
"@rollup/plugin-typescript": "^12.1.4",
|
|
42
|
+
"@types/node": "^22.19.1",
|
|
43
|
+
"rollup": "^4.40.0",
|
|
44
|
+
"rollup-plugin-dts": "^6.2.3",
|
|
45
|
+
"tslib": "^2.8.1",
|
|
46
|
+
"tsx": "^4.23.15",
|
|
47
|
+
"typescript": "^5.9.3"
|
|
48
|
+
}
|
|
49
|
+
}
|