Skip to content

Commit

Permalink
Merge pull request #1696 from SpareBank1/symbols-typings
Browse files Browse the repository at this point in the history
Legg til typescript typings for symbols
  • Loading branch information
HeleneKassandra authored Nov 3, 2023
2 parents 6887537 + 2a93040 commit 25656f8
Show file tree
Hide file tree
Showing 4 changed files with 3,348 additions and 1 deletion.
18 changes: 18 additions & 0 deletions packages/ffe-symbols-react/scripts/fetchSymbolNames.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Fetches all the icon names by using the codepoints in the font */
const fetchSymbolNames = async () => {
let folderNames = [];
const url =
'https://raw.githubusercontent.com/google/material-design-icons/master/variablefont/MaterialSymbolsRounded%5BFILL%2CGRAD%2Copsz%2Cwght%5D.codepoints';
const response = await fetch(url);
const data = await response.text();

const lines = data.split('\n');
const names = lines
.filter(line => line.trim() !== '')
.map(line => `'${line.split(' ')[0]}'`);
folderNames = [...folderNames, ...names];

return folderNames;
};

module.exports = fetchSymbolNames;
29 changes: 29 additions & 0 deletions packages/ffe-symbols-react/scripts/generateSymbolTypings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
This function is manually triggered, and will fetch all the names of the symbols.
The names are then gathered in an array that is used for typescript typings in the symbols component.
Important to only run this when we update the icon-font files, as we might sometimes be out of sync with whats
the latest in material icons.
*/
const path = require('path');
const fs = require('fs').promises;
const fetchSymbolNames = require('./fetchSymbolNames');

const writeToFile = async (filePath, content) => {
try {
await fs.writeFile(filePath, content);
console.log('File written successfully');
} catch (error) {
console.error('Error writing file:', error);
}
};

const generateSymbolTypings = async () => {
const symbolNames = await fetchSymbolNames();
const fileContent = `export type SymbolName = ${symbolNames.join(' | ')}`;
const filePath = path.join(__dirname, '../src/typings/symbolNames.ts');

await writeToFile(filePath, fileContent);
};

generateSymbolTypings();
3 changes: 2 additions & 1 deletion packages/ffe-symbols-react/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as React from 'react';
import { SymbolName } from './typings/symbolNames.ts';

export interface SymbolProps {
fill?: boolean;
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | '2xl';
weight?: 300 | 400 | 500;
icon: string;
icon: SymbolName;
className?: string;
ariaLabel?: string;
}
Expand Down
Loading

0 comments on commit 25656f8

Please sign in to comment.