Skip to content

Commit 69dd788

Browse files
authored
Google Fonts picker - remove need for google api (codesandbox#3050)
* remove need for google * only show 200 fonts for speed
1 parent 4255c53 commit 69dd788

File tree

7 files changed

+1019
-113
lines changed

7 files changed

+1019
-113
lines changed

packages/app/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
"@codesandbox/executors": "^0.1.0",
7373
"@codesandbox/template-icons": "^1.0.1",
7474
"@emmetio/codemirror-plugin": "^0.3.5",
75-
"@samuelmeuli/font-manager": "^1.2.0",
7675
"@sentry/webpack-plugin": "^1.8.0",
7776
"@styled-system/css": "^5.0.23",
7877
"@svgr/core": "^2.4.1",

packages/app/src/app/pages/Sandbox/Editor/Workspace/Dependencies/AddFont/FontPicker/List.tsx

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,46 @@
11
import React, { FunctionComponent, useState, useMemo } from 'react';
2-
import { Font } from '@samuelmeuli/font-manager';
32
import { List, SearchFonts, FontLI, FontFamily, Arrow } from './elements';
3+
import { fonts } from './fonts';
44

55
type Props = {
6-
fonts: Font[];
76
onSelection: (e: any) => void;
87
activeFontFamily: string;
9-
expanded: boolean;
108
};
119

1210
export const FontList: FunctionComponent<Props> = ({
13-
fonts,
1411
onSelection,
1512
activeFontFamily,
16-
expanded,
1713
}) => {
1814
const [searchTerm, setSearchTerm] = useState('');
15+
const usedFonts = fonts.slice(0, 200);
1916

2017
const updateSearch = (e: any) => setSearchTerm(e.target.value);
2118

22-
const getFontId = (fontFamily: string): string =>
23-
fontFamily.replace(/\s+/g, '-').toLowerCase();
24-
25-
const getFonts: Font[] = useMemo(
19+
const getFonts: string[] = useMemo(
2620
() =>
27-
fonts.filter(f =>
28-
f.family.toLowerCase().includes(searchTerm.trim().toLowerCase())
21+
usedFonts.filter(f =>
22+
f.toLowerCase().includes(searchTerm.trim().toLowerCase())
2923
),
30-
[fonts, searchTerm]
24+
[searchTerm, usedFonts]
3125
);
3226
return (
3327
<>
3428
<Arrow />
35-
<List expanded={expanded}>
29+
<List>
3630
<SearchFonts
3731
type="text"
3832
value={searchTerm}
3933
onChange={updateSearch}
4034
placeholder="Search Typefaces"
4135
/>
42-
{getFonts.map((font: Font) => (
36+
{getFonts.map((font: string) => (
4337
<FontLI
44-
key={font.family}
45-
onClick={onSelection}
46-
onKeyPress={onSelection}
38+
key={font}
39+
onClick={() => onSelection(font)}
40+
onKeyPress={() => onSelection(font)}
4741
>
48-
<FontFamily
49-
type="button"
50-
id={`font-button-${getFontId(font.family)}`}
51-
active={font.family === activeFontFamily}
52-
>
53-
{font.family}
42+
<FontFamily type="button" active={font === activeFontFamily}>
43+
{font}
5444
</FontFamily>
5545
</FontLI>
5646
))}

packages/app/src/app/pages/Sandbox/Editor/Workspace/Dependencies/AddFont/FontPicker/elements.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import styled, { css } from 'styled-components';
1+
import styled from 'styled-components';
22
import Color from 'color';
33

4+
const svg = props =>
5+
`data:image/svg+xml,%3Csvg width='7' height='4' viewBox='0 0 7 4' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M3.50007 4L1.27146e-07 1.35122e-07L7 -4.76837e-07L3.50007 4Z' fill='${
6+
props.theme.light ? 'black' : 'white'
7+
}'/%3E%3C/svg%3E%0A`;
8+
49
const makeDarker = ({ theme }) =>
510
Color(theme['input.background'])
611
.darken(theme.light ? 0.1 : 0.3)
@@ -80,13 +85,8 @@ export const List = styled.ul<{ expanded?: boolean }>`
8085
background-color: ${props => makeLighter(props)};
8186
width: 240px;
8287
z-index: 10;
83-
84-
${props =>
85-
props.expanded &&
86-
css`
87-
max-height: 130px;
88-
display: block;
89-
`}
88+
max-height: 130px;
89+
display: block;
9090
`;
9191

9292
export const SelectedFont = styled.button<{ done?: boolean }>`
@@ -105,23 +105,18 @@ export const SelectedFont = styled.button<{ done?: boolean }>`
105105
position: relative;
106106
box-sizing: border-box;
107107
outline: none;
108+
cursor: pointer;
108109
109-
${props =>
110-
props.done &&
111-
css`
112-
:after {
113-
content: '';
114-
background-image: url("${`data:image/svg+xml,%3Csvg width='7' height='4' viewBox='0 0 7 4' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M3.50007 4L1.27146e-07 1.35122e-07L7 -4.76837e-07L3.50007 4Z' fill='${
115-
props.theme.light ? 'black' : 'white'
116-
}'/%3E%3C/svg%3E%0A`}");
117-
width: 7px;
118-
height: 4px;
119-
position: absolute;
120-
right: 0.5rem;
121-
top: 50%;
122-
transform: translateY(-50%);
123-
}
124-
`}
110+
:after {
111+
content: '';
112+
background-image: url("${props => svg(props)}");
113+
width: 7px;
114+
height: 4px;
115+
position: absolute;
116+
right: 0.5rem;
117+
top: 50%;
118+
transform: translateY(-50%);
119+
}
125120
`;
126121

127122
export const Arrow = styled.div`

0 commit comments

Comments
 (0)