Reputation: 239
I've developing react typescript project and trying to load custom font. But I can see font is loaded in Network tab but font is still not changed to what I want. I tried many related post in StackOverflow but still the same. The last I did is this link. Here's my current code I added for font.
assets/fonts/globalfonts.ts
import { url } from 'inspector';
import {createGlobalStyle} from 'styled-components';
export const GlobalFonts = createGlobalStyle`
@font-face {
font-family: 'Trixie Plain';
font-style: normal;
font-weight: 400;
src: url("./assets/fonts/Trixie-Plain.otf") format('opentype')
}
`
src/components/TopBar/components/Nav.tsx
import React from 'react'
import { NavLink } from 'react-router-dom'
import styled from 'styled-components'
import { GlobalFonts } from '../../../assets/fonts/globalfonts'
const Nav: React.FC = () => {
return (
<StyledNav>
<GlobalFonts />
<StyledLink exact activeClassName="active" to="/">
Home
</StyledLink>
</StyledNav>
)
}
const StyledNav = styled.nav`
align-items: center;
display: flex;
font-family: 'Trixie Plain';
`
[
Upvotes: 1
Views: 5593
Reputation: 26
You need to define a declaration file d.ts as mentioned in the https://dev.to/anteronunes/comment/171a3
Upvotes: 1
Reputation: 1
Did you tried by creating rules in the webpack?
module.exports = {
module: {
rules: [
{
test: /\.(woff2|woff|eot|ttf|otf)$/,
use: ["file-loader"],
},
],
},
}
Upvotes: 0
Reputation: 5786
Can you try altering the globals font face as below?
export const GlobalFonts = createGlobalStyle`
@font-face {
font-family: TrixiePlain;
// rest of declaration
}
`
Note above the font-family value without space/quotes. Thus, in styled components:
const StyledNav = styled.nav`
align-items: center;
display: flex;
font-family: TrixiePlain;
`
Upvotes: 0