Skip to content

Commit 9f9901a

Browse files
Update Vue and Svelte packages
1 parent 06dbd2d commit 9f9901a

9 files changed

Lines changed: 64 additions & 53 deletions

File tree

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<script lang="ts">
2-
import { convertNodesWithConfig, renderNodeWithRoot } from './utils'
3-
import { kebabCase } from './utils'
4-
import type { ProIconAttributes } from './types'
2+
import type { IconNode, ProIconAttributes } from './types'
3+
import { convertNodesWithConfig, kebabCase } from './utils'
54
6-
type IconNode = [string, Record<string, string>, IconNode[]]
75
type Props = ProIconAttributes & {
86
icon: {
97
name: string
@@ -36,27 +34,38 @@ const customizationOptions = {
3634
cornerRadius,
3735
size,
3836
}
37+
const customizedNodes = convertNodesWithConfig(nodes, customizationOptions)
3938
40-
const rootNode: IconNode = [
41-
'svg',
42-
{
43-
width: size ?? 24,
44-
height: size ?? 24,
45-
xmlns: 'http://www.w3.org/2000/svg',
46-
viewBox: '0 0 24 24',
47-
fill: 'none',
48-
class: ['proicon', props?.class ?? ''].join(''),
49-
'data-proicon-id': kebabCase(name),
50-
...props,
51-
},
52-
[],
53-
]
54-
55-
const iconHtml = renderNodeWithRoot(
56-
convertNodesWithConfig(nodes, customizationOptions),
57-
rootNode,
58-
customizationOptions
59-
)
39+
// svelte-ignore non_reactive_update
40+
let className: string | any[] = 'proicon'
41+
if (props?.class) {
42+
if (typeof props.class == 'string') className += ` ${props.class}`
43+
else className = [className, props.class]
44+
}
6045
</script>
6146

62-
{@html iconHtml}
47+
<svg
48+
width={size ?? 24}
49+
height={size ?? 24}
50+
xmlns="http://www.w3.org/2000/svg"
51+
viewBox="0 0 24 24"
52+
fill="none"
53+
class={className}
54+
data-proicon-id={kebabCase(name)}
55+
{...props}
56+
>
57+
{#snippet node(tag, p, children)}
58+
<svelte:element this={tag} {...p}>
59+
{#each children as [tag2, p2, children2]}
60+
{@render node(tag2, p2, children2)}
61+
{/each}
62+
</svelte:element>
63+
{/snippet}
64+
{#each customizedNodes as [tag, p, children]}
65+
<svelte:element this={tag} {...p}>
66+
{#each children as [tag2, p2, children2]}
67+
{@render node(tag2, p2, children2)}
68+
{/each}
69+
</svelte:element>
70+
{/each}
71+
</svg>

packages/proicons-svelte/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { SVGAttributes } from 'svelte/elements'
22

3+
export type IconNode = [string, Record<string, any>, IconNode[]]
4+
35
export interface ProIconsOptions {
46
/** Determines the color of the icons. Defaults to `currentColor`. */
57
color?: string

packages/proicons-svelte/test/App.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ const increment = () => {
1919
</div>
2020

2121
<p></p>
22-
<AddSquareMultipleIcon size={48} strokeWidth={2} />
22+
<AddSquareMultipleIcon size={48} strokeWidth={2} class="myClass" />
2323
<ProIcon icon="Add" strokeWidth={2} color="red" />
2424
</main>

packages/proicons-vue/src/ProIcon.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { kebabCase, pascalCase } from '../../../src/rename'
2+
import { kebabCase, pascalCase } from './rename'
33
import { ProIconAttributes } from './types'
44
import * as icons from './icons'
55

packages/proicons-vue/src/createIcon.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { type FunctionalComponent, h } from 'vue'
2-
import { kebabCase, kebabToPascalCase, pascalToCamelCase } from '../../../src/rename'
3-
import { convertNodesWithConfig } from '../../../src/renderNodes'
2+
import { kebabCase, kebabToPascalCase, pascalToCamelCase } from './rename'
3+
import { convertNodesWithConfig } from './renderNodes'
44
import { type ProIconAttributes } from './types'
55

66
export type IconNode = [string, Record<string, string>, IconNode[]]
@@ -32,7 +32,7 @@ export function createIcon(
3232
xmlns: 'http://www.w3.org/2000/svg',
3333
viewBox: '0 0 24 24',
3434
fill: 'none',
35-
class: ['proicon', props.class].flat(),
35+
class: ['proicon', props.class],
3636
'data-proicon-id': kebabCase(name),
3737
...props,
3838
},
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../src/rename.ts
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../src/renderNodes.ts

packages/proicons-vue/tsconfig.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
"rootDir": "./src"
3030
},
3131
"include": [
32-
"**/*.ts",
33-
"**/*.tsx",
34-
"**/*.vue"
32+
"src/**/*.ts",
33+
"src/**/*.tsx",
34+
"src/**/*.vue"
3535
]
3636
}

src/renderNodes.ts

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,37 +23,34 @@ export function convertNodesWithConfig(nodes: IconNode[], options?: ProIconsOpti
2323
strokeCaps: ["stroke-linejoin"],
2424
strokeJoin: ["stroke-linecap"],
2525
cornerRadius: ["rx"],
26-
strokeFilledElements: undefined,
2726
}
2827

2928
if (!options) return nodes
3029

3130
return nodes.map(node => {
32-
const children = node[2]
31+
const [_, props, children] = node
3332

3433
for (const [optionK, svgAttrs] of Object.entries(attributeKey)) {
35-
if (svgAttrs) {
36-
for (const s of svgAttrs) {
37-
if (node[1][s] && options[optionK]) {
38-
node[1][s] = options[optionK]
39-
}
34+
for (const s of svgAttrs) {
35+
if (props[s] && options[optionK]) {
36+
props[s] = options[optionK]
4037
}
4138
}
4239
}
4340

4441
// Outlining
4542
if (
46-
!Object.hasOwn(node[1], 'stroke-width') &&
47-
Object.hasOwn(node[1], 'fill') &&
48-
node[1].fill != 'none' &&
49-
node[1].stroke != 'none' &&
50-
node[1]['stroke-width'] !== '0' &&
43+
!Object.hasOwn(props, 'stroke-width') &&
44+
Object.hasOwn(props, 'fill') &&
45+
props.fill != 'none' &&
46+
props.stroke != 'none' &&
47+
props['stroke-width'] !== '0' &&
5148
(options?.strokeWidth ?? 0) > defaultStroke
5249
) {
53-
node[1]['stroke-width'] = (options.strokeWidth - defaultStroke).toString()
54-
node[1].stroke = node[1].fill
55-
node[1]['stroke-linecap'] = options.strokeCaps ?? 'round'
56-
node[1]['stroke-linejoin'] = options.strokeJoin ?? 'round'
50+
props['stroke-width'] = (options.strokeWidth - defaultStroke).toString()
51+
props.stroke = props.fill
52+
props['stroke-linecap'] = options.strokeCaps ?? 'round'
53+
props['stroke-linejoin'] = options.strokeJoin ?? 'round'
5754
}
5855

5956
if (children.length) {
@@ -65,16 +62,17 @@ export function convertNodesWithConfig(nodes: IconNode[], options?: ProIconsOpti
6562

6663
export function renderNodeWithRoot(nodes: IconNode[], rootNode: IconNode, options: ProIconsOptions): string {
6764
const root = structuredClone(rootNode)
68-
root[2].push(...nodes)
65+
const [_, props, children] = root
66+
children.push(...nodes)
6967

7068
if (options?.size) {
71-
root[1].width = options.size.toString()
72-
root[1].height = options.size.toString()
69+
props.width = options.size.toString()
70+
props.height = options.size.toString()
7371
}
7472

7573
if (options?.attributes) {
7674
for (const [k, v] of Object.entries(options.attributes)) {
77-
root[1][k] = v
75+
props[k] = v
7876
}
7977
}
8078

0 commit comments

Comments
 (0)