Skip to content
Draft
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
21 changes: 19 additions & 2 deletions plugins/vite.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import path from 'path'
import fs from 'fs/promises'

import { ILLUS_OPTIONS, SPRITES_OPTIONS, processSvgWithOutput } from '../scripts/svg.js'
import {
ILLUS_OPTIONS,
SPRITES_OPTIONS,
getSvgIds,
processSvgWithOutput,
replaceAvailableSvgs,
} from '../scripts/svg.js'
import { forFile, __dirname } from '../scripts/utils.js'
import { fetchStatusAssetLogos, replaceAssetLogosSource } from '../scripts/asset-logos.js'
import {
fetchMetricsRestrictions,
replaceDefaultMetricsRestrictionsSource,
} from '../scripts/metrics-restrictions/index.js'

export function WebkitSvg() {
export async function WebkitSvg() {
const base = __dirname()
const isLibPackage = base.includes('node_modules')

Expand All @@ -19,9 +25,20 @@ export function WebkitSvg() {
const staticDir = './static/webkit/'
const spritesStaticDir = './static/webkit/sprites/'

const svgIds = await getSvgIds()

return {
name: 'webkit-svg',

transform(src: string, id: string) {
if (id.includes('/core/Svg/ids.ts')) {
return {
code: replaceAvailableSvgs(src, svgIds),
map: null,
}
}
},

async buildStart() {
if (isLibPackage) {
const root = path.resolve(base, '..')
Expand Down
19 changes: 7 additions & 12 deletions scripts/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
fetchMetricsRestrictions,
replaceDefaultMetricsRestrictionsSource,
} from './metrics-restrictions/index.js'
import { ILLUS_OPTIONS, SPRITES_OPTIONS, processSvgWithOutput, replaceSvgIdsType } from './svg.js'
import { combineSvgsToSprite, getSvgIds, replaceAvailableSvgs, replaceSvgIdsType } from './svg.js'

const MAIN_BRANCH = 'next'
const RELEASE_BRANCH = 'lib-release'
Expand Down Expand Up @@ -204,18 +204,13 @@ async function replaceSrcImports() {
}

async function processSvg() {
const ids = []
async function process(path, options) {
await forFile(path, async (entry) => {
const id = entry.replace('./dist/icons/', '').replace('./dist/illus/', '').replace('.svg', '')
ids.push(id)

processSvgWithOutput(entry, './dist/', './dist/sprites/', options, './dist/')
})
}
const [ids] = await Promise.all([getSvgIds(), combineSvgsToSprite()])

await forFile(['./dist/**/core/Svg/ids.js'], (entry) => {
const file = fs.readFileSync(entry)

await process(['./dist/icons/**/*.svg'], SPRITES_OPTIONS)
await process(['./dist/illus/**/*.svg'], ILLUS_OPTIONS)
fs.writeFileSync(entry, replaceAvailableSvgs(file.toString(), ids))
})

await forFile(['./dist/**/core/Svg/types.d.ts'], (entry) => {
const file = fs.readFileSync(entry)
Expand Down
50 changes: 50 additions & 0 deletions scripts/svg.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import path from 'path'
import fs from 'fs'

import { forFile } from './utils.js'

/**
*
* @param {string} path
Expand Down Expand Up @@ -110,3 +112,51 @@ export const replaceSvgIdsType = (src, ids) =>
'type TSvgId = string',
`type TSvgId = ${ids.map((id) => `"${id}"`).join(' | ')} | (string & {})`,
)

export async function combineSvgsToSprite() {
/**
*
* @param {string[]} path
* @param {any} options
*/
async function process(path, options) {
await forFile(path, async (entry) => {
processSvgWithOutput(entry, './dist/', './dist/sprites/', options, './dist/')
})
}

await process(['./dist/icons/**/*.svg'], SPRITES_OPTIONS)
await process(['./dist/illus/**/*.svg'], ILLUS_OPTIONS)
}

export async function getSvgIds() {
/**
* @type {string[]}
*/
const ids = []

/**
*
* @param {string[]} path
*/
async function process(path) {
await forFile(path, async (entry) => {
const id = entry.replace('./dist/icons/', '').replace('./dist/illus/', '').replace('.svg', '')
ids.push(id)
})
}

await process(['./dist/icons/**/*.svg'])
await process(['./dist/illus/**/*.svg'])

return ids
}

/**
*
* @param {string} src - Source code
* @param {Array<string>} svgs - Stringified JSON array
* @returns {string}
*/
export const replaceAvailableSvgs = (src, svgs) =>
src.replace('AVAILABLE_IDS = []', `AVAILABLE_IDS = ${JSON.stringify(svgs)}`)
5 changes: 5 additions & 0 deletions src/lib/icons/search-12.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions src/lib/icons/search-20.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions src/lib/icons/search.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 5 additions & 3 deletions src/lib/icons/sidebar.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@

<section class="flex flex-1 flex-col gap-3">
<Input
icon="search"
icon="search-12"
placeholder="Search for trending words"
oninput={onInput}
onkeyup={onKeyUp}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
{#snippet content({ close })}
<section class="flex flex-col gap-2">
<Input
icon="search"
icon="search-12"
oninput={onInput}
onkeyup={onKeyUp}
placeholder="Search assets"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</script>

<section class="flex flex-col gap-3">
<Input icon="search" placeholder="Search for watchlist" oninput={onInput} onkeyup={onKeyUp} />
<Input icon="search-12" placeholder="Search for watchlist" oninput={onInput} onkeyup={onKeyUp} />

<section class="flex flex-col gap-3">
{#each filteredWatchlists as watchlist}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<section class="flex flex-col gap-3">
<Input
icon="search"
icon="search-12"
placeholder="Search for metric"
oninput={onSearchInput}
onkeyup={onSearchKeyUp}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ui/app/ListOfAssets/Layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<Input
class="bg-inherit"
inputClass="md:py-2.5"
icon="search"
icon="search-12"
placeholder="Search project"
oninput={onInput}
onkeyup={onKeyUp}
Expand Down
23 changes: 19 additions & 4 deletions src/lib/ui/core/Button/Button.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import { useDeviceCtx } from '$lib/ctx/device/index.svelte.js'
import { cn } from '$ui/utils/index.js'
import Svg, { type TSvgId } from '$ui/core/Svg/index.js'
import Svg, { type TSvgId, checkSvgIdAvailable } from '$ui/core/Svg/index.js'

type TButtonVariants = VariantProps<typeof button>

Expand Down Expand Up @@ -68,7 +68,22 @@

const isPhone = $derived(device.$.isPhone)
const size = $derived(initialSize ?? (isPhone ? 'lg' : 'md'))
const iconSize = $derived(initialIconSize ?? (size === 'md' || size === 'lg' ? 16 : 12))

const { icon: sizedIcon, size: autoIconSize } = $derived.by(() => {
if (!icon) return {}
if (size === 'md' || size === 'lg') return { icon, size: undefined }

const smallId = `${icon}-12`
const isSmallIconAvailable = checkSvgIdAvailable(`${icon}-12`)

if (isSmallIconAvailable) return { icon: smallId, size: undefined }

return { icon, size: 12 }
})

const iconSize = $derived(
initialIconSize === 'auto' || !initialIconSize ? autoIconSize : initialIconSize,
)

const button = tv({
base: 'flex items-center cursor-pointer gap-2 rounded-md',
Expand Down Expand Up @@ -212,8 +227,8 @@
className,
)}
>
{#if icon}
<Svg id={icon} w={iconSize} h={iconHeight} illus={iconIllus} />
{#if sizedIcon}
<Svg id={sizedIcon} w={iconSize} h={iconHeight} illus={iconIllus} />
{/if}

{#if children}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ui/core/Input/UniversalInput.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
as,
placeholder = '',
icon,
iconSize = 12,
iconSize,
disabled,
left,
right,
Expand Down
14 changes: 12 additions & 2 deletions src/lib/ui/core/Svg/Svg.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@

import { cn } from '$ui/utils/index.js'

let { id, class: className = '', w = 16, h = w, illus = false, style = '' }: TProps = $props()
const { id, class: className = '', w, h, illus = false, style = '' }: TProps = $props()

const defaultWidth = getSizeFromId()

const width = $derived(w ?? defaultWidth)
const height = $derived(h ?? width)

function getSizeFromId() {
const size = Number(id.split('-').at(-1))
return Number.isFinite(size) ? size : 16
}
</script>

<svg
style="width:{w}px;height:{h}px;min-width:{w}px;{style}"
style="width:{width}px;height:{height}px;min-width:{width}px;{style}"
style:--svg-stroke="currentColor"
class={cn(className)}
>
Expand Down
8 changes: 8 additions & 0 deletions src/lib/ui/core/Svg/ids.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { TSvgId } from './types.js'

// Replace in build time
const AVAILABLE_IDS = [] as TSvgId[]

const SVG_IDS = new Set<TSvgId>(AVAILABLE_IDS)

export const checkSvgIdAvailable = (id: string) => SVG_IDS.has(id)
1 change: 1 addition & 0 deletions src/lib/ui/core/Svg/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { default } from './Svg.svelte'
export type { TSvgId } from './types.js'
export { checkSvgIdAvailable } from './ids.js'
6 changes: 3 additions & 3 deletions src/stories/Design System - Core UI/Input/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

<Input placeholder="Placeholder"></Input>

<Input icon="search"></Input>
<Input icon="search-12"></Input>

<Input icon="search">
<Input icon="search-12">
{#snippet left()}
left
{/snippet}
Expand All @@ -26,7 +26,7 @@
{/snippet}
</Input>

<Input icon="search" disabled>
<Input icon="search-12" disabled>
{#snippet left()}
left
{/snippet}
Expand Down