Skip to content

Commit

Permalink
fix: variable work sans font
Browse files Browse the repository at this point in the history
  • Loading branch information
theo-mesnil committed Jul 26, 2023
1 parent 9a812a7 commit f4081dc
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jobs:
yarn build:core
- aws-s3/sync:
from: packages/IconFont/fonts
to: s3://welcome-ui/fonts/icon-font/$ICON_FONT_HASH/
to: s3://welcome-ui/public/fonts/icon-font/$ICON_FONT_HASH/
arguments: |
--metadata GitCommit=$CIRCLE_SHA1 --delete
- aws-s3/sync:
Expand Down
5 changes: 1 addition & 4 deletions docs/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ export function App({ component: Component, pageProps }) {
}, [themeStorage])

return (
<WuiProvider
reactRootId="__next"
theme={createTheme({ fontsUrl: 'https://cdn.welcometothejungle.com', ...globalTheme })}
>
<WuiProvider reactRootId="__next" theme={createTheme(globalTheme)}>
<MDXProvider components={MDXComponents}>
<GlobalStyle />
<Head />
Expand Down
8 changes: 8 additions & 0 deletions docs/pages/theming/customize.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,14 @@ export default function Root() {
}
```

## Change fonts path url

If you wan to change the url path for the fonts, you can change the property `fontsUrl` on `createTheme()`. On our case at Welcome to the jungle, we want to have the fonts from the same domain name as our main website. By default is on welcome-ui.com domain.

```jsx live=false
const theme = createTheme({ fontsUrl: 'https://cdn.welcometothejungle.com/fonts', ...yourTheme })
```

## All theme values

Here are all the [possible values for your theme](https://github.com/WTTJ/welcome-ui/blob/master/packages/Core/theme/core.ts).<br />These will be merged with the default theme.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"scripts": {
"build:core": "./scripts/core.sh",
"build:docs": "lerna run doc --concurrency 2",
"build:packages": "lerna run build --ignore docs",
"build:packages": "lerna run build --ignore docs --ignore @welcome-ui/core",
"build:types": "lerna run types",
"build": "yarn webfont:build && yarn build:core && yarn build:packages && yarn build:types && yarn build:docs",
"check:audit": "/bin/bash -c 'yarn audit --level critical; [[ $? -ge 16 ]] && exit 1 || exit 0'",
Expand Down
7 changes: 3 additions & 4 deletions packages/Core/src/theme/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ const DEFAULT_FONT_FAMILY = 'Work Sans'
const DEFAULT_FONT_SIZE = 16
const DEFAULT_LETTER_SPACING = '-0.019rem'
const DEFAULT_LINE_HEIGHT = 1.15
const FONTS_URL = 'https://cdn.welcome-ui.com'
const FONTS_URL = 'https://cdn.welcome-ui.com/fonts'
const HEADING_FONT_FAMILY = 'welcome-font'
const ICON_FONT_FAMILY = 'welcome-icon-font'

export type ThemeFontsUrl =
| 'https://cdn.welcome-ui.com'
| 'https://cdn.welcometothejungle.com'
| 'https://cdn.welcome-ui.com/fonts'
| 'https://cdn.welcometothejungle.com/fonts'
| string

export type Options = {
Expand Down Expand Up @@ -115,7 +115,6 @@ export const createTheme = (options: Options = {}): WuiTheme => {
theme.fontWeights = fontWeights
theme.letterSpacings = getLetterSpacings(theme)
theme.fonts = getFonts(defaultFontFamily, headingFontFamily, iconFontFamily)

theme.borderWidths = borderWidths

theme.screens = screens
Expand Down
51 changes: 20 additions & 31 deletions packages/Core/src/theme/fonts.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { WuiTheme } from './types'

type FontFaceExtension = 'woff' | 'woff2'

type FontFace = {
display?: FontDisplay
isVariable?: boolean
stretch?: string
style?: string
url: string
weight?: string
style?: 'italic'
display: 'swap' | 'block'
extensions: FontFaceExtension[]
extensions?: string[]
}

export type ThemeFontFaces = {
Expand All @@ -19,57 +19,46 @@ export type ThemeFontFaces = {
export const fontFaces = (theme: WuiTheme): ThemeFontFaces => ({
'welcome-font': [
{
url: `${theme.fontsUrl}/fonts/welcome-font-regular`,
url: `${theme.fontsUrl}/welcome-font-regular`,
weight: '400',
display: 'swap',
extensions: ['woff2', 'woff'],
},
{
url: `${theme.fontsUrl}/fonts/welcome-font-medium`,
url: `${theme.fontsUrl}/welcome-font-medium`,
weight: '500',
display: 'swap',
extensions: ['woff2', 'woff'],
},
{
url: `${theme.fontsUrl}/fonts/welcome-font-bold`,
url: `${theme.fontsUrl}/welcome-font-bold`,
weight: '600',
display: 'swap',
extensions: ['woff2', 'woff'],
},
{
url: `${theme.fontsUrl}/fonts/welcome-font-regular-italic`,
weight: '400',
url: `${theme.fontsUrl}/welcome-font-regular-italic`,
style: 'italic',
display: 'swap',
extensions: ['woff2', 'woff'],
weight: '400',
},
{
url: `${theme.fontsUrl}/fonts/welcome-font-medium-italic`,
weight: '500',
url: `${theme.fontsUrl}/welcome-font-medium-italic`,
style: 'italic',
display: 'swap',
extensions: ['woff2', 'woff'],
weight: '500',
},
{
url: `${theme.fontsUrl}/fonts/welcome-font-bold-italic`,
weight: '600',
url: `${theme.fontsUrl}/welcome-font-bold-italic`,
style: 'italic',
display: 'swap',
extensions: ['woff2', 'woff'],
weight: '600',
},
],
'welcome-icon-font': [
{
url: `${theme.fontsUrl}/fonts/icon-font/__ICON_FONT_HASH__/welcome-icon-font`,
url: `${theme.fontsUrl}/icon-font/__ICON_FONT_HASH__/welcome-icon-font`,
display: 'block',
extensions: ['woff2', 'woff'],
},
],
'Work Sans': [
{
url: `${theme.fontsUrl}/fonts/work-sans-variable`,
display: 'block',
extensions: ['woff2', 'woff'],
url: `${theme.fontsUrl}/work-sans-variable`,
isVariable: true,
stretch: '75% 125%',
style: 'oblique 0deg 20deg',
weight: '400 500 600',
},
],
})
2 changes: 0 additions & 2 deletions packages/Core/src/utils/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createGlobalStyle, css, th } from '@xstyled/styled-components'

import { fonts } from './font'
import { normalizeStyle, resetStyles } from './reset'
import { workSans } from './work-sans'

const baseBoxSizing = css`
* {
Expand Down Expand Up @@ -33,7 +32,6 @@ function baseFonts() {
export const GlobalStyle = createGlobalStyle<{ useReset?: boolean }>(
({ useReset }) => css`
${normalizeStyle};
${workSans()}
${fonts()};
${baseFonts()};
${useReset ? resetStyles : baseBoxSizing};
Expand Down
60 changes: 35 additions & 25 deletions packages/Core/src/utils/font.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,51 @@ import { css } from '@xstyled/styled-components'

import { WuiTheme } from '../theme/types'

function getFormat(extension: string) {
return extension === 'ttf' ? 'truetype' : extension
}

function getUrl(url: string, extension: string) {
return `url('${url}.${extension}') format('${getFormat(extension)}')`
}

type Descriptor = {
name: string
type FontVariation = {
url: string
extensions: string[]
display?: string
display?: FontDisplay
weight?: string
style?: string
isVariable?: boolean
extension?: string
}

type Font = {
name: string
variation: FontVariation
}

function getSrc(descriptor: Descriptor) {
return descriptor.extensions
.map((extension: string) => getUrl(descriptor.url, extension))
.join(', ')
function getSource(
url: FontVariation['url'],
extension: FontVariation['extension'],
isVariable: FontVariation['isVariable']
) {
const formattedExtension = extension === 'ttf' ? 'truetype' : extension

/** variable icon font */
if (isVariable) {
return `url('${url}.${extension}') format('${formattedExtension}-variations');`
}

return `url('${url}.${extension}') format('${formattedExtension}')`
}

function getFont(descriptor: Descriptor) {
function getFont({
name,
variation: { display = 'swap', extension = 'woff2', isVariable, style, url, weight },
}: Font) {
return css`
@font-face {
font-family: ${descriptor.name};
src: ${getSrc(descriptor)};
font-display: ${descriptor.display || 'fallback'};
${descriptor.weight &&
font-family: ${name};
src: ${getSource(url, extension, isVariable)};
font-display: ${display};
${weight &&
css`
font-weight: ${descriptor.weight};
font-weight: ${weight};
`}
${descriptor.style &&
${style &&
css`
font-style: ${descriptor.style};
font-style: ${style};
`}
}
`
Expand All @@ -47,7 +56,8 @@ export const fonts =
() =>
({ theme }: { theme: WuiTheme }): ReturnType<typeof css> => {
if (!theme || !theme.fontFaces) return null

return Object.entries(theme.fontFaces).map(([name, variations]) =>
variations.map(variation => getFont({ name, ...variation }))
variations.map(variation => getFont({ name, variation }))
)
}
16 changes: 0 additions & 16 deletions packages/Core/src/utils/work-sans.ts

This file was deleted.

4 changes: 2 additions & 2 deletions scripts/webfont-deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ FONT_FILE="$FONTS_DIR/welcome-icon-font.woff"
if [[ -f "$FONT_FILE" ]]; then
ICON_FONT_HASH=$(sha1sum $FONT_FILE | awk '{ print $1 }')

echo "Copying fonts to s3://welcome-ui/fonts/icon-font/$ICON_FONT_HASH/"
echo "Copying fonts to s3://welcome-ui/public/fonts/icon-font/$ICON_FONT_HASH/"

aws s3 sync $FONTS_DIR s3://welcome-ui/fonts/icon-font/$ICON_FONT_HASH/
aws s3 sync $FONTS_DIR s3://welcome-ui/public/fonts/icon-font/$ICON_FONT_HASH/

echo "Copying fonts to s3://wttj-production/fonts/icon-font/$ICON_FONT_HASH/"

Expand Down

0 comments on commit f4081dc

Please sign in to comment.