Skip to content

Commit

Permalink
Nedlastning av valgte ikoner i png-format og fargevelger (#973)
Browse files Browse the repository at this point in the history
* converterer svg til png format

* gitignore update

* oppdatert build steps package.json

* kan nå laste ned alle ikoner i png format

* zipper svg og png riktig

* git fix

* oppdaterte ikon svg zip

* info om bruk av png ikoner

* lint fix

* la til png zip

* fjernet -png fra git

* publish fix(2)

* build fix

* implementert fargevelger

* fullført implementering av fargevelger og png nedlastning

* oppdatert navn pngs

* lint fix

* oppdatert ikoner for svg pakke

* diverse UU forbedringer

* småfiks for mobilvisning og fargevisning

* lint fix

* fikser state for valgt farge

* Flyttet svg.zip til buildsteg
  • Loading branch information
KenAJoh committed Feb 5, 2021
1 parent c892cfe commit 53b65b0
Show file tree
Hide file tree
Showing 14 changed files with 326 additions and 57 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,8 @@ packages/**/css
utilities/**/css

*/static/*.zip
*/static/png
@navikt/ds-icons/NAV-ikonpakke-png.zip
@navikt/ds-icons/NAV-ikonpakke-svg.zip
*/**/NAV-ikonpakke-png.zip
*/**/NAV-ikonpakke-svg.zip
Binary file removed @navikt/ds-icons/NAV-ikonpakke-svg.zip
Binary file not shown.
10 changes: 0 additions & 10 deletions @navikt/ds-icons/figma-api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const path = require("path");
const pLimit = require("p-limit");
const rimraf = require("rimraf");
const startCase = require("lodash.startcase");
const zipdir = require("zip-dir");

const generateMetadata = (iconNodesArr) => {
return iconNodesArr
Expand Down Expand Up @@ -97,15 +96,6 @@ const main = async () => {
console.log(`\nCould not download ${misses.length} icons\n`);
} else {
console.log("\nDownloaded all icons from Figma successfully!\n");
await zipdir(
"./svg/",
{ saveTo: "NAV-ikonpakke-svg.zip" },
function (err, buffer) {
if (err) {
console.error(err);
}
}
);
}
};

Expand Down
17 changes: 1 addition & 16 deletions @navikt/ds-icons/figma-api/pngConverter.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
const fs = require("fs");
const sharp = require("sharp");
const rimraf = require("rimraf");
const zipdir = require("zip-dir");
const pLimit = require("p-limit");

const zipPng = async () => {
await zipdir(
"./png/",
{ saveTo: "NAV-ikonpakke-png.zip" },
function (err, buffer) {
if (err) {
console.error(err);
}
}
);
};

const convertToPng = async () => {
const inputDir = "./svg/";
const outDir = "./png/";
Expand All @@ -34,7 +21,7 @@ const convertToPng = async () => {
fs.readdirSync(inputDir).map((file) => {
return limit(() =>
sizes.forEach((size) => {
const density = (72 * size) / 16;
const density = (100 * size) / 16;
sharp(inputDir + file, { density })
.resize(size)
.png()
Expand All @@ -44,8 +31,6 @@ const convertToPng = async () => {
);
})
);

await zipPng();
console.log("Finished converting icons to .png format");
};

Expand Down
16 changes: 16 additions & 0 deletions @navikt/ds-icons/figma-api/zipIcons.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const zipdir = require("zip-dir");

const zipDir = async (dir, name) => {
await zipdir(dir, { saveTo: name }, function (err, buffer) {
if (err) {
console.error(err);
}
});
};

try {
zipDir("./png/", "NAV-ikonpakke-png.zip");
zipDir("./svg/", "NAV-ikonpakke-svg.zip");
} catch (e) {
console.error(e);
}
4 changes: 2 additions & 2 deletions @navikt/ds-icons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
"dl-icons": "FILE_KEY=UmEVH3pZ71uJPsSz9ilP3Y node -r dotenv/config figma-api/index.js dotenv_config_path=figma-api/.env",
"create-icons": "svgr --typescript --ref --icon --title-prop --replace-attr-values \"#3E3832=currentColor\" -d src svg",
"convert-icons": "node figma-api/pngConverter.js",
"clean": "rm -rf src && rm -rf lib && rm -rf png && rm -rf NAV-ikonpakke-png.zip",
"build": "npm run clean && npm run create-icons && tsc && npm run convert-icons"
"clean": "rm -rf src && rm -rf lib && rm -rf png && rm -rf NAV-ikonpakke-png.zip && rm -rf NAV-ikonpakke-svg.zip",
"build": "npm run clean && npm run create-icons && tsc && npm run convert-icons && node figma-api/zipIcons.js"
},
"repository": {
"type": "git",
Expand Down
8 changes: 5 additions & 3 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,18 @@
"react-use-keypress": "^1.0.1",
"rehype-slug": "^4.0.1",
"rehype-toc": "^3.0.2",
"@navikt/ds-css": "0.1.0-beta.1"
"@navikt/ds-css": "0.1.0-beta.1",
"canvg": "^3.0.7"
},
"devDependencies": {
"prettier": "2.2.1"
},
"scripts": {
"build:gatsby": "gatsby build",
"build:gatsby": "npm run copy:static && gatsby build",
"develop": "gatsby develop",
"format": "prettier --write \"**/*.{js,jsx,ts,tsx,json,md}\"",
"start": "npx copyfiles -E -f ../@navikt/ds-icons/*.zip ./static && npm run develop",
"copy:static": "npx copyfiles -E -f ../@navikt/ds-icons/*.zip ./static",
"start": "npm run copy:static && npm run develop",
"serve": "gatsby serve",
"clean": "gatsby clean",
"test": "echo \"Write tests! -> https://gatsby.dev/unit-testing\" && exit 1",
Expand Down
10 changes: 9 additions & 1 deletion website/src/components/code/Code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface CodeProps {
onClick?: (event: React.SyntheticEvent) => void;
noCopy?: boolean;
popupUnder?: boolean;
arialabel?: string;
}

export const InlineCode = ({ children, className, ...props }: CodeProps) => (
Expand Down Expand Up @@ -95,6 +96,7 @@ const Code = ({
className,
noCopy,
popupUnder = false,
arialabel,
...props
}: CodeProps) => {
const [anchor, setAnchor] = useState(undefined);
Expand Down Expand Up @@ -128,7 +130,13 @@ const Code = ({
<figure
className={classnames({ "code-example": !noCopy })}
role={noCopy ? "figure" : "button"}
aria-label={noCopy ? "Kode-eksempel" : "Kopier kode-eksempel"}
aria-label={
arialabel
? arialabel
: noCopy
? "Kode-eksempel"
: "Kopier kode-eksempel"
}
{...getNewProps()}
>
<pre className="language-">
Expand Down
50 changes: 50 additions & 0 deletions website/src/components/color-switch/ColorSwitch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import React, { useState } from "react";
import cl from "classnames";
import { Close } from "@navikt/ds-icons";
import "./index.css";
import "@navikt/ds-css/button/index.css";

const cls = (selected, none = false) => {
return cl("colorswitch__button", "navds-button", "navds-button--secondary", {
"colorswitch__button--none": none,
"colorswitch__button--selected": selected,
});
};

interface ColorSwtichProps {
onChange: (color) => void;
}

const ColorSwitch = ({ onChange, ...props }: ColorSwtichProps) => {
const colors = ["#FFFFFF", "#000000", "#0067C5"];
const [selectedColor, setSelectedColor] = useState("currentColor");

const onColorChange = (color) => {
setSelectedColor(color);
onChange(color);
};
return (
<div className="colorswitch">
<button
className={cls(selectedColor === "currentColor", true)}
onClick={() => onColorChange("currentColor")}
aria-label="Sett farge til currentColor"
aria-pressed={selectedColor === "currentColor"}
>
<Close className="colorswitch__closeicon" />
</button>
{colors.map((c) => (
<button
key={c}
className={cls(selectedColor === c)}
style={{ backgroundColor: c }}
onClick={() => onColorChange(c)}
aria-label={`Sett farge til hex ${c}`}
aria-pressed={selectedColor === c}
/>
))}
</div>
);
};

export default ColorSwitch;
33 changes: 33 additions & 0 deletions website/src/components/color-switch/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
.colorswitch {
display: flex;
align-items: center;
}
.colorswitch__button.navds-button {
height: 2rem;
width: 2rem;
padding: 0;
border-radius: 0.5rem;
margin: 0.2rem;
box-shadow: var(--navds-shadow-hover);
transform: translateY(0.2rem);
border: 1px solid var(--navds-color-darkgray);
}

.colorswitch__button--none.navds-button {
display: flex;
align-items: center;
justify-content: center;
font-size: 2rem;
background-color: #ffffff;
}

.colorswitch__closeicon {
color: #a13a28;
}

.colorswitch__button--selected.navds-button {
transform: translateY(-0.4rem);
transform: rotateZ(5deg);
box-shadow: var(--navds-shadow-focus);
border: 1px solid #f1f1f1;
}
30 changes: 30 additions & 0 deletions website/src/components/icons/GeneratePng.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const JSZip = require("jszip");
const canvg = require("canvg");

export const generatePngZip = async (svgstring: string, name: string) => {
const sizes = [16, 24, 128, 256];

const zip = new JSZip();
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");

sizes.forEach((size) => {
let svg = svgstring.replace(/width="[^"]*"/, `width="${size}px"`);
svg = svg.replace(/height="[^"]*"/, `height="${size}px"`);

const v = canvg.Canvg.fromString(ctx, svg);
v.start();

const data = canvas
.toDataURL("image/png", 1)
.replace(/^data:image\/\w+;base64,/, "");

v.stop();

zip
.folder(`${name}-png`)
.file(`${name}-${size}px.png`, data, { base64: true });
});

return await zip.generateAsync({ type: "blob" });
};

0 comments on commit 53b65b0

Please sign in to comment.