Skip to content

Commit 121359f

Browse files
authored
Merge pull request #1 from Frontendland/feature/icons-component
Add icons component
2 parents b410f72 + 84dfd3d commit 121359f

27 files changed

+164
-87
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"files": [
3030
"components",
31-
"public",
31+
"icons",
3232
"scss",
3333
"astro.d.ts",
3434
"astro.js",

scripts/build.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
import fs from 'fs'
2+
import buildImports from './buildImports.js'
3+
import buildTypes from './buildTypes.js'
24

35
const folders = {
4-
'public': 'dist/public',
6+
'src/icons': 'dist/icons',
57
'src/components': 'dist/components',
68
'src/scss': 'dist/scss'
79
}
810

911
const files = {
10-
'src/astro.d.ts': 'dist/astro.d.ts',
11-
'src/astro.js': 'dist/astro.js',
12-
'src/svelte.d.ts': 'dist/svelte.d.ts',
13-
'src/svelte.js': 'dist/svelte.js',
14-
'src/react.d.ts': 'dist/react.d.ts',
15-
'src/react.js': 'dist/react.js',
1612
'README.md': 'dist/README.md',
1713
'LICENSE': 'dist/LICENSE',
1814
'package.json': 'dist/package.json'
@@ -40,4 +36,12 @@ Object.keys(files).forEach(key => {
4036
})
4137
})
4238

39+
fs.writeFileSync('dist/astro.js', buildImports('astro'))
40+
fs.writeFileSync('dist/svelte.js', buildImports('svelte'))
41+
fs.writeFileSync('dist/react.js', buildImports('tsx'))
42+
43+
fs.writeFileSync('dist/astro.d.ts', buildTypes('astro'))
44+
fs.writeFileSync('dist/svelte.d.ts', buildTypes('svelte'))
45+
fs.writeFileSync('dist/react.d.ts', buildTypes('react'))
46+
4347
console.log('✅ Package built')

scripts/buildImports.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import fs from 'fs'
2+
3+
const buildImports = extension => {
4+
const components = fs.readdirSync('src/components')
5+
6+
return components.map(component => {
7+
return `import ${component}Component from './components/${component}/${component}.${extension}'`
8+
}).join('\n')
9+
+ `\n\n`
10+
+ components.map(component => `export const ${component} = ${component}Component`).join('\n')
11+
}
12+
13+
export default buildImports

scripts/buildTypes.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import fs from 'fs'
2+
3+
const buildTypes = type => {
4+
const components = fs.readdirSync('src/components')
5+
6+
return `
7+
declare module 'webcoreui/${type}' {
8+
${components.map(component => {
9+
return `import type { ${component}Props } from './components/${component}/${component.toLowerCase()}'`
10+
}).join('\n\t')}
11+
12+
${components.map(component => {
13+
return `export function ${component}(_props: ${component}Props): any`
14+
}).join('\n\t')}
15+
}
16+
`
17+
}
18+
19+
export default buildTypes

src/astro.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/astro.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/components/Accordion/Accordion.astro

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
import type { AccordionProps } from './accordion'
3+
import Icon from '../Icon/Icon.astro'
34
45
interface Props extends AccordionProps {}
56
@@ -9,16 +10,11 @@ const {
910
---
1011

1112
<ul data-id="accordion">
12-
{items.map(item => (
13+
{items.map((item: AccordionProps['items'][0]) => (
1314
<li>
1415
<div class="accordion-title">
1516
{item.title}
16-
<img
17-
src="/icons/arrow-down.svg"
18-
alt="GitHub"
19-
width={15}
20-
height={15}
21-
/>
17+
<Icon type="arrow-down" size={15} />
2218
</div>
2319
<div class="accordion-wrapper">
2420
<div class="accordion-content">

src/components/Accordion/Accordion.svelte

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<script lang="ts">
22
import type { AccordionProps } from './accordion'
3+
import ArrowDown from '../../icons/arrow-down.svg?raw'
34
45
export let items: AccordionProps['items']
56
@@ -22,12 +23,7 @@
2223
on:click={() => toggle(index)}
2324
>
2425
{item.title}
25-
<img
26-
src="/icons/arrow-down.svg"
27-
alt="GitHub"
28-
width={15}
29-
height={15}
30-
/>
26+
{@html ArrowDown}
3127
</div>
3228
<div class="accordion-wrapper">
3329
<div class="accordion-content">

src/components/Accordion/Accordion.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React, { useState } from 'react'
22
import type { AccordionProps } from './accordion'
3+
import ArrowDown from '../../icons/arrow-down.svg?raw'
34
import './accordion.scss'
45

5-
export const Accordion = ({ items }: AccordionProps) => {
6+
const Accordion = ({ items }: AccordionProps) => {
67
const [state, setState] = useState(Array(items.length).fill(false))
78

89
const toggle = (index: number) => {
@@ -19,15 +20,8 @@ export const Accordion = ({ items }: AccordionProps) => {
1920
<div
2021
className={state[index] ? 'accordion-title open' : 'accordion-title'}
2122
onClick={() => toggle(index)}
22-
>
23-
{item.title}
24-
<img
25-
src="/icons/arrow-down.svg"
26-
alt="GitHub"
27-
width={15}
28-
height={15}
29-
/>
30-
</div>
23+
dangerouslySetInnerHTML={{ __html: `${item.title} ${ArrowDown}` }}
24+
/>
3125
<div className="accordion-wrapper">
3226
<div className="accordion-content">
3327
<div dangerouslySetInnerHTML={{ __html: item.content }} />
@@ -38,3 +32,5 @@ export const Accordion = ({ items }: AccordionProps) => {
3832
</ul>
3933
)
4034
}
35+
36+
export default Accordion

src/components/Accordion/accordion.scss

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,16 @@ ul {
2525
align-items: center;
2626
cursor: pointer;
2727

28-
img {
28+
svg {
2929
@include Transition(transform);
30-
filter: brightness(.7);
30+
color: #BBB;
31+
width: 15px;
32+
height: 15px;
33+
pointer-events: none;
3134
}
3235

3336
&.open {
34-
img {
37+
svg {
3538
transform: rotate(180deg);
3639
}
3740

src/components/Button/Button.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22
import type { ButtonProps } from './button'
33
import './button.scss'
44

5-
export const Button = ({
5+
const Button = ({
66
theme,
77
bold,
88
href,
@@ -30,3 +30,5 @@ export const Button = ({
3030
</button>
3131
)
3232
}
33+
34+
export default Button

src/components/Card/Card.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import React from 'react'
2+
3+
const Card = () => <div />
4+
5+
export default Card

src/components/Card/card.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type CardProps = {}

src/components/Icon/Icon.astro

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
import type { IconProps } from './icon'
3+
4+
interface Props extends IconProps {}
5+
6+
const {
7+
type,
8+
size = 24,
9+
color
10+
} = Astro.props
11+
12+
const { default: markup } = await import(`../../icons/${type}.svg?raw`)
13+
const icon = markup
14+
.replace('width="24"', `width=${size}`)
15+
.replace('height="24"', color
16+
? `height=${size} color=${color}`
17+
: `height=${size}`)
18+
---
19+
20+
<Fragment set:html={icon} />

src/components/Icon/Icon.svelte

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<script lang="ts">
2+
import type { IconProps } from './icon'
3+
4+
import Github from '../../icons/github.svg?raw'
5+
import ArrowDown from '../../icons/arrow-down.svg?raw'
6+
7+
export let type: IconProps['type']
8+
export let size: IconProps['size'] = 24
9+
export let color: IconProps['color']
10+
11+
const iconMap = {
12+
'github': Github,
13+
'arrow-down': ArrowDown
14+
}
15+
16+
const icon = iconMap[type]
17+
.replace('width="24"', `width=${size}`)
18+
.replace('height="24"', color
19+
? `height=${size} color=${color}`
20+
: `height=${size}`)
21+
</script>
22+
23+
{@html icon}

src/components/Icon/Icon.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react'
2+
import type { IconProps } from './icon'
3+
4+
import Github from '../../icons/github.svg?raw'
5+
import ArrowDown from '../../icons/arrow-down.svg?raw'
6+
7+
const iconMap = {
8+
'github': Github,
9+
'arrow-down': ArrowDown
10+
}
11+
12+
const Icon = ({
13+
type,
14+
size = 24,
15+
color
16+
}: IconProps) => {
17+
const icon = iconMap[type]
18+
.replace('width="24"', `width=${size}`)
19+
.replace('height="24"', color
20+
? `height=${size} color=${color}`
21+
: `height=${size}`)
22+
23+
return <div dangerouslySetInnerHTML={{ __html: icon }} />
24+
}
25+
26+
export default Icon

src/components/Icon/icon.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export type IconProps = {
2+
type: string
3+
size?: number
4+
color?: string
5+
}
Lines changed: 1 addition & 1 deletion
Loading
Lines changed: 2 additions & 2 deletions
Loading

src/pages/accordion.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import ComponentWrapper from '@static/ComponentWrapper.astro'
44
55
import AstroAccordion from '@components/Accordion/Accordion.astro'
66
import SvelteAccorion from '@components/Accordion/Accordion.svelte'
7-
import { Accordion as ReactAccordion } from '@components/Accordion/Accordion.tsx'
7+
import ReactAccordion from '@components/Accordion/Accordion.tsx'
88
99
const accordionItems = [{
1010
title: 'Do you offer support?',

src/pages/button.astro

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import Layout from '@static/Layout.astro'
33
import ComponentWrapper from '@static/ComponentWrapper.astro'
44
5+
import Icon from '@components/Icon/Icon.astro'
56
import AstroButton from '@components/Button/Button.astro'
67
import SvelteButton from '@components/Button/Button.svelte'
7-
import { Button as ReactButton } from '@components/Button/Button.tsx'
8+
import ReactButton from '@components/Button/Button.tsx'
89
---
910

1011
<Layout>
@@ -43,14 +44,9 @@ import { Button as ReactButton } from '@components/Button/Button.tsx'
4344
<AstroButton disabled>Disabled</AstroButton>
4445
</ComponentWrapper>
4546

46-
<ComponentWrapper title="Wiht Icon">
47+
<ComponentWrapper title="With Icon">
4748
<AstroButton theme="secondary">
48-
<img
49-
src="/icons/github.svg"
50-
alt="GitHub"
51-
width={20}
52-
height={20}
53-
/>
49+
<Icon type="github" size={20} />
5450
With Icon
5551
</AstroButton>
5652
</ComponentWrapper>

src/pages/index.astro

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import CardWrapper from '@static/CardWrapper.astro'
44
55
import Button from '@components/Button/Button.astro'
66
import Accordion from '@components/Accordion/Accordion.astro'
7+
import Icon from '@components/Icon/Icon.astro'
78
---
89

910
<Layout>
@@ -22,7 +23,7 @@ import Accordion from '@components/Accordion/Accordion.astro'
2223
href="https://github.com/Frontendland/webcoreui" target="_blank"
2324
theme="secondary"
2425
>
25-
<img src="/icons/github.svg" alt="GitHub" width={20} height={20} />
26+
<Icon type="github" size={20} />
2627
GitHub
2728
</Button>
2829
</div>

src/react.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/react.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/svelte.d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/svelte.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

0 commit comments

Comments
 (0)