[@next/font] Need "FIXED" font family name #43452
Replies: 12 comments 7 replies
-
You can define your font as a css variable and use that name in your styles. import localFont from "@next/font/local";
const myFont = localFont({
variable: "--my-font",
src: [...],
}
...
<div style={{fontFamily: "var(--my-font)"}}>...</div> |
Beta Was this translation helpful? Give feedback.
-
@Fredkiss3 I know it, but it's not a solution for my issue. I'm developing a Lucidchart-like diagramming tool and users need to use font names like "Inter", "Roboto" for Text shape objects which have to be drawn in HTML5 Canvas element. Assumes that I used the css variable (e.g. {
"type": "Text",
"x": 100, "y": 100, "w": 50, "h": 70,
"fontFamily": "inter-239873", "fontSize": 12,
...
} When I rebuild the Next.js project then the css variable may be changed to like |
Beta Was this translation helpful? Give feedback.
-
@niklauslee One workaround idea could be to keep a mapping of {
"type": "Text",
"x": 100, "y": 100, "w": 50, "h": 70,
"fontFamily": "--font-inter", "fontSize": 12,
...
} |
Beta Was this translation helpful? Give feedback.
-
Is there any workaround for this? I have third party components that come in with a |
Beta Was this translation helpful? Give feedback.
-
I have similar concerns for use with a 3rd party visual CMS like Builder.io. It automatically imports your fonts, but if the font-family is variable, it means that the font-family that is assigned when applying styling won't necessarily match every time because it's a variable name. That means you could randomly end up with unstyled content because the font-family name changed during a deploy. I understand the variable name is probably required due to how these are built under the covers, but there has to be some way to establish a fixed name, doesn't there? |
Beta Was this translation helpful? Give feedback.
-
Assuming you've abandoned IE 11 and can use CSS Variables, consider updating / overriding impacted CSS rules: /* BEFORE */
font-family: Inter, Helvetica, san-serif;
/* AFTER */
font-family: var(--font-inter, 'Inter'), Helvetica, san-serif; ... setting const inter = Inter({
display: 'swap',
subsets: ['latin'],
variable: '--font-inter',
weight: ['400', '500', '600', '700', '800'],
}); Finally, use value of export default function MyApp({ Component, pageProps }) {
return (
<main className={inter.variable}>
<Component {...pageProps} />
</main>
);
} FWIW, the official Tailwind integration advice for /** @type {import('tailwindcss').Config} */
module.exports = {
content: [
'./pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}',
],
theme: {
extend: {
fontFamily: {
sans: ['var(--font-inter)'],
mono: ['var(--font-roboto-mono)'],
},
},
},
plugins: [],
}; Footnotes
|
Beta Was this translation helpful? Give feedback.
-
It seems crazy to me that this isn't supported. According to the docs you should be able to specify |
Beta Was this translation helpful? Give feedback.
-
Would definitely love to see this supported. It's very helpful for component libraries shipping font-family styles with custom fonts and asking product teams to import the font assets. In that case you don't have access to the css to switch to a variable or use a generated font name. |
Beta Was this translation helpful? Give feedback.
-
Had some time so figured out a fix that could work locally, so planning to open a PR after I read through all the contribution guidelines 😅 What I'm doing in my local branch right now is:
What do you think about this approach? Right now it's just ignoring the hashing if the flag is true, but I can also see minor potential improvements here:
Edit: |
Beta Was this translation helpful? Give feedback.
-
Opened a PR for this: #53608 Had some issues running the next/font tests locally so still trying to figure out why they can't start the Next instance but would be great to get folks interested in this feature check the PR as well. |
Beta Was this translation helpful? Give feedback.
-
For anyone here trying to use next fonts for writing text on a canvas, heres how i did it: const caveat = Caveat({ subsets: ["latin"] }) ctx.font = |
Beta Was this translation helpful? Give feedback.
-
This is now resolved in https://github.com/vercel/next.js/releases/tag/v14.3.0-canary.64 |
Beta Was this translation helpful? Give feedback.
-
Describe the feature you'd like to request
I'm using
<canvas>
and drawing some shapes and texts with font name (e.g. "Inter", "Roboto", etc_) and the shape's data including font name is stored in database. So I want to use@next/font
with the FIXED font family name like"Inter"
,"MyFont"
instead of using auto-generated name like"inter-23987176"
.Describe the solution you'd like
It would be nice if we have an option for the FIXED font family name as below:
Describe alternatives you've considered
I've tried "declarations" option with "font-family" property, but it didn't work.
Beta Was this translation helpful? Give feedback.
All reactions