Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fonts #1039

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open

Fonts #1039

wants to merge 18 commits into from

Conversation

florian-lefebvre
Copy link
Member

Summary

Have first-party support for fonts in Astro:

// astro config
export default defineConfig({
	fonts: {
		families: ["Roboto", "Lato"]
	}
})
---
// layouts/Layout.astro
import { Font } from "astro:fonts"
---
<head>
	<Font family="Inter" preload />
	<Font family="Lato" />
	<style>
		h1 {
			font-family: var(--astro-font-inter);
		}
		p {
			font-family: var(--astro-font-lato);
		}
	</style>
</head>

Links

@florian-lefebvre florian-lefebvre changed the title [WIP] Fonts Fonts Oct 22, 2024
@florian-lefebvre florian-lefebvre marked this pull request as ready for review October 22, 2024 14:39
@florian-lefebvre florian-lefebvre mentioned this pull request Oct 22, 2024
@lilnasy
Copy link
Contributor

lilnasy commented Oct 22, 2024

How does preloading pick the src when the font provides multiple files for multiple unicode ranges? See IBM Plex Mono on font source for example.

What's the reasoning behind considering subsetting a non-goal? It helps a lot when the font participates in LCP and is trivial to do when using google fonts directly. Moving to astro fonts in that case would be a downgrade.

@florian-lefebvre
Copy link
Member Author

How does preloading pick the src when the font provides multiple files for multiple unicode ranges? See IBM Plex Mono on font source for example.

I think we can't choose the src to pick automatically, so that would preload everything? Not sure, have ideas in mind?

What's the reasoning behind considering subsetting a non-goal? It helps a lot when the font participates in LCP and is trivial to do when using google fonts directly. Moving to astro fonts in that case would be a downgrade.

Only automatic subsetting is a non goal (eg. by analyzing the static content), subsetting is actually supported. I'll clarify the non goal

proposals/0052-fonts.md Outdated Show resolved Hide resolved
proposals/0052-fonts.md Show resolved Hide resolved
<Font family='Lato' />
<style>
h1 {
font-family: var(--astro-font-inter);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's gonna be important to make sure those variables are accessible correctly to things like Tailwind. It's a bit unclear with this API because the Tailwind CSS would be imported in the frontmatter, whereas the fonts are in the document.

Perhaps Vite will re-order it to make it work, not sure.

Copy link
Member Author

@florian-lefebvre florian-lefebvre Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well since it's just some css, I don't think it will cause any issues? In Tailwind 3, you'd have to reference like so:

import defaultTheme from 'tailwindcss/defaultTheme'

export default {
  theme: {
    extend: {
      fontFamily: {
        sans: ['var(--astro-font-inter)', ...defaultTheme.fontFamily.sans],
      },
    }
  }
}

provider: "adobe",
weights: [200, 700],
styles: ["italic"],
subsets: [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would love an example with opentype fonts features

Copy link
Member Author

@florian-lefebvre florian-lefebvre Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that would be one key per setting? Something like the following:

{
	fontKerning: "'kern' 1",
	fontVariantAlternates: "1"
	// etc...
}

MDN docs: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_fonts/OpenType_fonts_guide

myCustomFontProvider(),
],
defaults: {
provider: "adobe",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how would you know the name of the provider here? Will it be... typed (👀) automatically based on what you put in providers?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Thanks to defineConfig() being generic now, we can do a lot of cool stuff here. Providers would be easily typed


| Path | Example (weight) | Advantage | Downside |
| --------- | ------------------- | --------------------- | --------------------------------------------------------------------------------- |
| Minimal | Only include `400` | Lightweight | People will probably struggle by expecting all weights to be available by default |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm team just 400, Google Fonts also gives you only 400 when you unselect the "give all options" setting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants