Skip to content

Add icons component #1

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

Merged
merged 3 commits into from
Jun 16, 2024
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"files": [
"components",
"public",
"icons",
"scss",
"astro.d.ts",
"astro.js",
Expand Down
18 changes: 11 additions & 7 deletions scripts/build.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import fs from 'fs'
import buildImports from './buildImports.js'
import buildTypes from './buildTypes.js'

const folders = {
'public': 'dist/public',
'src/icons': 'dist/icons',
'src/components': 'dist/components',
'src/scss': 'dist/scss'
}

const files = {
'src/astro.d.ts': 'dist/astro.d.ts',
'src/astro.js': 'dist/astro.js',
'src/svelte.d.ts': 'dist/svelte.d.ts',
'src/svelte.js': 'dist/svelte.js',
'src/react.d.ts': 'dist/react.d.ts',
'src/react.js': 'dist/react.js',
'README.md': 'dist/README.md',
'LICENSE': 'dist/LICENSE',
'package.json': 'dist/package.json'
Expand Down Expand Up @@ -40,4 +36,12 @@ Object.keys(files).forEach(key => {
})
})

fs.writeFileSync('dist/astro.js', buildImports('astro'))
fs.writeFileSync('dist/svelte.js', buildImports('svelte'))
fs.writeFileSync('dist/react.js', buildImports('tsx'))

fs.writeFileSync('dist/astro.d.ts', buildTypes('astro'))
fs.writeFileSync('dist/svelte.d.ts', buildTypes('svelte'))
fs.writeFileSync('dist/react.d.ts', buildTypes('react'))

console.log('✅ Package built')
13 changes: 13 additions & 0 deletions scripts/buildImports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import fs from 'fs'

const buildImports = extension => {
const components = fs.readdirSync('src/components')

return components.map(component => {
return `import ${component}Component from './components/${component}/${component}.${extension}'`
}).join('\n')
+ `\n\n`
+ components.map(component => `export const ${component} = ${component}Component`).join('\n')
}

export default buildImports
19 changes: 19 additions & 0 deletions scripts/buildTypes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import fs from 'fs'

const buildTypes = type => {
const components = fs.readdirSync('src/components')

return `
declare module 'webcoreui/${type}' {
${components.map(component => {
return `import type { ${component}Props } from './components/${component}/${component.toLowerCase()}'`
}).join('\n\t')}

${components.map(component => {
return `export function ${component}(_props: ${component}Props): any`
}).join('\n\t')}
}
`
}

export default buildTypes
5 changes: 0 additions & 5 deletions src/astro.d.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/astro.js

This file was deleted.

10 changes: 3 additions & 7 deletions src/components/Accordion/Accordion.astro
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
import type { AccordionProps } from './accordion'
import Icon from '../Icon/Icon.astro'

interface Props extends AccordionProps {}

Expand All @@ -9,16 +10,11 @@ const {
---

<ul data-id="accordion">
{items.map(item => (
{items.map((item: AccordionProps['items'][0]) => (
<li>
<div class="accordion-title">
{item.title}
<img
src="/icons/arrow-down.svg"
alt="GitHub"
width={15}
height={15}
/>
<Icon type="arrow-down" size={15} />
</div>
<div class="accordion-wrapper">
<div class="accordion-content">
Expand Down
8 changes: 2 additions & 6 deletions src/components/Accordion/Accordion.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script lang="ts">
import type { AccordionProps } from './accordion'
import ArrowDown from '../../icons/arrow-down.svg?raw'

export let items: AccordionProps['items']

Expand All @@ -22,12 +23,7 @@
on:click={() => toggle(index)}
>
{item.title}
<img
src="/icons/arrow-down.svg"
alt="GitHub"
width={15}
height={15}
/>
{@html ArrowDown}
</div>
<div class="accordion-wrapper">
<div class="accordion-content">
Expand Down
16 changes: 6 additions & 10 deletions src/components/Accordion/Accordion.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, { useState } from 'react'
import type { AccordionProps } from './accordion'
import ArrowDown from '../../icons/arrow-down.svg?raw'
import './accordion.scss'

export const Accordion = ({ items }: AccordionProps) => {
const Accordion = ({ items }: AccordionProps) => {
const [state, setState] = useState(Array(items.length).fill(false))

const toggle = (index: number) => {
Expand All @@ -19,15 +20,8 @@ export const Accordion = ({ items }: AccordionProps) => {
<div
className={state[index] ? 'accordion-title open' : 'accordion-title'}
onClick={() => toggle(index)}
>
{item.title}
<img
src="/icons/arrow-down.svg"
alt="GitHub"
width={15}
height={15}
/>
</div>
dangerouslySetInnerHTML={{ __html: `${item.title} ${ArrowDown}` }}
/>
<div className="accordion-wrapper">
<div className="accordion-content">
<div dangerouslySetInnerHTML={{ __html: item.content }} />
Expand All @@ -38,3 +32,5 @@ export const Accordion = ({ items }: AccordionProps) => {
</ul>
)
}

export default Accordion
9 changes: 6 additions & 3 deletions src/components/Accordion/accordion.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,16 @@ ul {
align-items: center;
cursor: pointer;

img {
svg {
@include Transition(transform);
filter: brightness(.7);
color: #BBB;
width: 15px;
height: 15px;
pointer-events: none;
}

&.open {
img {
svg {
transform: rotate(180deg);
}

Expand Down
4 changes: 3 additions & 1 deletion src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react'
import type { ButtonProps } from './button'
import './button.scss'

export const Button = ({
const Button = ({
theme,
bold,
href,
Expand Down Expand Up @@ -30,3 +30,5 @@ export const Button = ({
</button>
)
}

export default Button
5 changes: 5 additions & 0 deletions src/components/Card/Card.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react'

const Card = () => <div />

export default Card
1 change: 1 addition & 0 deletions src/components/Card/card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type CardProps = {}
20 changes: 20 additions & 0 deletions src/components/Icon/Icon.astro
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
import type { IconProps } from './icon'

interface Props extends IconProps {}

const {
type,
size = 24,
color
} = Astro.props

const { default: markup } = await import(`../../icons/${type}.svg?raw`)
const icon = markup
.replace('width="24"', `width=${size}`)
.replace('height="24"', color
? `height=${size} color=${color}`
: `height=${size}`)
---

<Fragment set:html={icon} />
23 changes: 23 additions & 0 deletions src/components/Icon/Icon.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<script lang="ts">
import type { IconProps } from './icon'

import Github from '../../icons/github.svg?raw'
import ArrowDown from '../../icons/arrow-down.svg?raw'

export let type: IconProps['type']
export let size: IconProps['size'] = 24
export let color: IconProps['color']

const iconMap = {
'github': Github,
'arrow-down': ArrowDown
}

const icon = iconMap[type]
.replace('width="24"', `width=${size}`)
.replace('height="24"', color
? `height=${size} color=${color}`
: `height=${size}`)
</script>

{@html icon}
26 changes: 26 additions & 0 deletions src/components/Icon/Icon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React from 'react'
import type { IconProps } from './icon'

import Github from '../../icons/github.svg?raw'
import ArrowDown from '../../icons/arrow-down.svg?raw'

const iconMap = {
'github': Github,
'arrow-down': ArrowDown
}

const Icon = ({
type,
size = 24,
color
}: IconProps) => {
const icon = iconMap[type]
.replace('width="24"', `width=${size}`)
.replace('height="24"', color
? `height=${size} color=${color}`
: `height=${size}`)

return <div dangerouslySetInnerHTML={{ __html: icon }} />
}

export default Icon
5 changes: 5 additions & 0 deletions src/components/Icon/icon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export type IconProps = {
type: string
size?: number
color?: string
}
2 changes: 1 addition & 1 deletion public/icons/arrow-down.svg → src/icons/arrow-down.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions public/icons/github.svg → src/icons/github.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/pages/accordion.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ComponentWrapper from '@static/ComponentWrapper.astro'

import AstroAccordion from '@components/Accordion/Accordion.astro'
import SvelteAccorion from '@components/Accordion/Accordion.svelte'
import { Accordion as ReactAccordion } from '@components/Accordion/Accordion.tsx'
import ReactAccordion from '@components/Accordion/Accordion.tsx'

const accordionItems = [{
title: 'Do you offer support?',
Expand Down
12 changes: 4 additions & 8 deletions src/pages/button.astro
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import Layout from '@static/Layout.astro'
import ComponentWrapper from '@static/ComponentWrapper.astro'

import Icon from '@components/Icon/Icon.astro'
import AstroButton from '@components/Button/Button.astro'
import SvelteButton from '@components/Button/Button.svelte'
import { Button as ReactButton } from '@components/Button/Button.tsx'
import ReactButton from '@components/Button/Button.tsx'
---

<Layout>
Expand Down Expand Up @@ -43,14 +44,9 @@ import { Button as ReactButton } from '@components/Button/Button.tsx'
<AstroButton disabled>Disabled</AstroButton>
</ComponentWrapper>

<ComponentWrapper title="Wiht Icon">
<ComponentWrapper title="With Icon">
<AstroButton theme="secondary">
<img
src="/icons/github.svg"
alt="GitHub"
width={20}
height={20}
/>
<Icon type="github" size={20} />
With Icon
</AstroButton>
</ComponentWrapper>
Expand Down
3 changes: 2 additions & 1 deletion src/pages/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CardWrapper from '@static/CardWrapper.astro'

import Button from '@components/Button/Button.astro'
import Accordion from '@components/Accordion/Accordion.astro'
import Icon from '@components/Icon/Icon.astro'
---

<Layout>
Expand All @@ -22,7 +23,7 @@ import Accordion from '@components/Accordion/Accordion.astro'
href="https://github.com/Frontendland/webcoreui" target="_blank"
theme="secondary"
>
<img src="/icons/github.svg" alt="GitHub" width={20} height={20} />
<Icon type="github" size={20} />
GitHub
</Button>
</div>
Expand Down
5 changes: 0 additions & 5 deletions src/react.d.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/react.js

This file was deleted.

5 changes: 0 additions & 5 deletions src/svelte.d.ts

This file was deleted.

7 changes: 0 additions & 7 deletions src/svelte.js

This file was deleted.

Loading