Skip to content
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

[icons-test] load dynamic of icons #471

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
18 changes: 18 additions & 0 deletions apps/exampleapp/esbuild.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { transform } = require("@svgr/core")
const url = require("postcss-url")
// this function generates app props based on package.json and propSecrets.json
const appProps = require("../../helpers/appProps")
const path = require("path")

if (!/.+\/.+\.js/.test(pkg.module))
throw new Error(
Expand Down Expand Up @@ -42,6 +43,23 @@ const build = async () => {
await fs.rm(outdir, { recursive: true, force: true })
await fs.mkdir(outdir, { recursive: true })

// copy folder with fs.cpsync the material design icons to build folder
// we need to resolve the node package in node_modules

const iconsPath = path.resolve(
path.dirname(
require.resolve("@material-design-icons/svg/filled/account_circle.svg")
),
"../"
)

await fs.cp(`${iconsPath}/filled`, `${outdir}/svg/filled`, {
recursive: true,
})
await fs.cp(`${iconsPath}/outlined`, `${outdir}/svg/outlined`, {
recursive: true,
})

// build app
let ctx = await esbuild.context({
bundle: true,
Expand Down
3 changes: 2 additions & 1 deletion apps/exampleapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"url-state-provider": "*",
"util": "^0.12.4",
"utils": "*",
"zustand": "4.3.7"
"zustand": "4.3.7",
"@material-design-icons/svg": "^0.14.4"
},
"scripts": {
"test": "jest",
Expand Down
5 changes: 5 additions & 0 deletions apps/exampleapp/src/components/AppContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import ModalManager from "./ModalManager"
import PanelManager from "./PanelManager"
import Peaks from "./peaks/Peaks"
import WelcomeView from "./WelcomeView"
import DynamicLoadIcon from "./DynamicLoadIcon"

const AppContent = (props) => {
const { setTabIndex, setCurrentModal } = useGlobalsActions()
Expand Down Expand Up @@ -57,6 +58,7 @@ const AppContent = (props) => {
<TabList>
<Tab>Peaks</Tab>
<Tab>Tab Two</Tab>
<Tab>Tab Three</Tab>
</TabList>

<TabPanel>
Expand Down Expand Up @@ -93,6 +95,9 @@ const AppContent = (props) => {
</Select>
</Container>
</TabPanel>
<TabPanel>
<DynamicLoadIcon />
</TabPanel>
</MainTabs>
<ModalManager />
</Container>
Expand Down
108 changes: 108 additions & 0 deletions apps/exampleapp/src/components/DynamicLoadIcon.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import React, { useEffect, useRef, useState } from "react"
import {
Breadcrumb,
BreadcrumbItem,
Container,
Button,
MainTabs,
Tab,
TabList,
TabPanel,
Select,
SelectOption,
FormRow,
TextInput,
} from "juno-ui-components"

// from camelcase to underscore
// https://stackoverflow.com/questions/4149276/javascript-camelcase-to-regular-form
const camelToSnake = (str) =>
str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`)

const MaterialIcon = ({ name, width, height }) => {
const ref = useRef()

useEffect(() => {
if (!ref.current) return

fetch(new URL(`./svg/filled/${name}.svg`, import.meta.url))
.then((r) => {
return r.text()
})
.then((text) => {
ref.current.innerHTML = text
ref.current.children[0].setAttribute("width", width)
ref.current.children[0].setAttribute("height", height)
ref.current.replaceWith(ref.current.children[0])
})
}, [ref.current, name, height, width])

return <div ref={ref}></div>
}

const DynamicLoadIcon = () => {
const [iconComponent, setIconComponent] = useState(null)
const [iconAttributes, setIconAttributes] = useState({
name: "account_circle",
width: 24,
height: 24,
alt: "",
title: "",
})
const [svg, setSvg] = useState(null)

return (
<>
<Container px={false} py={true}>
<FormRow>
<TextInput
label="Name"
value={iconAttributes.name}
placeholder="Icon name"
onChange={(event) => {
setIconAttributes({ ...iconAttributes, name: event.target.value })
}}
/>
</FormRow>
<FormRow>
<TextInput
label="Height"
placeholder="Icon height"
onChange={(event) => {
setIconAttributes({
...iconAttributes,
height: event.target.value,
})
}}
/>
</FormRow>
<FormRow>
<TextInput
label="Width"
placeholder="Icon width"
onChange={(event) => {
setIconAttributes({
...iconAttributes,
width: event.target.value,
})
}}
/>
</FormRow>
<FormRow>
<TextInput label="Alt" placeholder="Icon alt" />
</FormRow>
<FormRow>
<TextInput label="Title" placeholder="Icon title" />
</FormRow>
</Container>
{/* {iconComponent && iconComponent} */}
<MaterialIcon
name={iconAttributes.name}
width={iconAttributes.width}
height={iconAttributes.height}
/>
</>
)
}

export default DynamicLoadIcon
10 changes: 6 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.