<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: coding</title>
    <description>The latest articles on DEV Community by coding (@coding).</description>
    <link>https://dev.to/coding</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F813813%2F8f3bab74-8b23-4e80-b9d6-b45c08453a75.png</url>
      <title>DEV Community: coding</title>
      <link>https://dev.to/coding</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/coding"/>
    <language>en</language>
    <item>
      <title>Using a custom font with Remix Run and Chakra UI</title>
      <dc:creator>coding</dc:creator>
      <pubDate>Sat, 12 Feb 2022 02:49:25 +0000</pubDate>
      <link>https://dev.to/coding/using-a-custom-font-with-remix-run-and-chakra-ui-72d</link>
      <guid>https://dev.to/coding/using-a-custom-font-with-remix-run-and-chakra-ui-72d</guid>
      <description>&lt;p&gt;Recently, I needed to install a custom font in a site using Remix Run front end framework. The documentation that Chakra has regarding fonts did not work for me. So if it doesn't work for you, here's how to do that.&lt;/p&gt;

&lt;p&gt;This guide should also work if you are not using Chakra UI and only want to use css styling.&lt;/p&gt;

&lt;p&gt;In this example I am using a self hosted font and using the Chakra theme to style the &lt;code&gt;&amp;lt;Text&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;Heading&amp;gt;&lt;/code&gt; tags. But you can also add a class in the css file to fontstyle any text.&lt;/p&gt;

&lt;p&gt;Go to &lt;a href="https://google-webfonts-helper.herokuapp.com/fonts" rel="noopener noreferrer"&gt;Google Web Fonts Helper&lt;/a&gt; and choose the font you need. &lt;a href="https://fonts.google.com/specimen/Raleway" rel="noopener noreferrer"&gt;Raleway&lt;/a&gt; is the font used as the example in this guide.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fycdw3xxki4d2ys3wq9wq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fycdw3xxki4d2ys3wq9wq.png" alt="Image description" width="800" height="558"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Download the font files. If you choose modern browsers, you will get .woff and .woff2 files.&lt;/p&gt;

&lt;p&gt;Place these files inside a new "raleway" folder: app/public/fonts/raleway&lt;/p&gt;

&lt;p&gt;Create a .css file. For instance: index.css located in app/styles directory. Inside this css file, place the &lt;br&gt;
&lt;code&gt;@font-face&lt;/code&gt; code from the fonts helper.&lt;/p&gt;

&lt;p&gt;Optional: you can also specify a new css class so that you can style any text with this font.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@font-face {
    font-family: 'Raleway';
    font-style: normal;
    font-weight: 400;
    src: local(''),
         url('/fonts/raleway/raleway-v26-latin-regular.woff2') format('woff2'), /* Chrome 26+, Opera 23+, Firefox 39+ */
         url('/fonts/raleway/raleway-v26-latin-regular.woff') format('woff'); /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */

.raleway {
    font-family: "Raleway";
}
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Chakra UI uses themes. Make a new theme.tsx file, in your /app directory for example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { extendTheme, theme as defaultTheme } from "@chakra-ui/react";

const theme = extendTheme({
    ...defaultTheme,
    colors:{
        ...defaultTheme.colors,
    },
    fonts:{
        heading: "'Raleway', sans-serif;",
        body: "'Raleway', sans-serif;",
    },
})

export default theme
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the entry point of the application, root.tsx file (where the ChakraProvider tag is), add the theme to the tag.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default function App() {
  return (
    &amp;lt;Document&amp;gt;
     &amp;lt;ChakraProvider theme={theme}&amp;gt;
       &amp;lt;CSSReset/&amp;gt;
       &amp;lt;Outlet /&amp;gt;
     &amp;lt;/ChakraProvider&amp;gt;
    &amp;lt;/Document&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Import the css file into the file for the webpage you want to style. Use the links() function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import styles from "~/styles/index.css";
import {
  Links,LiveReload,Meta,Outlet,Scripts,ScrollRestoration,useCatch,
  useLoaderData
} from "remix";
import { Heading, Text } from '@chakra-ui/react'


export const links = () =&amp;gt; {
  return [{ rel: "stylesheet", href: styles }];
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you use the Chakra UI &lt;code&gt;&amp;lt;Heading&amp;gt;&lt;/code&gt; or &lt;code&gt;&amp;lt;Text&amp;gt;&lt;/code&gt; tag (corresponding to the heading: and body: in the theme.tsx) the font should be the custom font family displaying on the webpage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// app/routes/index.tsx
export default function Index() {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;h1 className="raleway"&amp;gt;ABCDEFG Test of Font using 
        className
        &amp;lt;/h1&amp;gt;
        &amp;lt;Heading&amp;gt;ABCDEFG Test of Font from Heading&amp;lt;/Heading&amp;gt;
        &amp;lt;Text&amp;gt;ABCDEFG Test of font from text&amp;lt;/Text&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you created a css class, you can also see your custom font by using the className, e.g. &lt;code&gt;&amp;lt;p className='raleway'&amp;gt;Test&amp;lt;/p&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ep4vf6jujpiylj8mxnb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ep4vf6jujpiylj8mxnb.png" alt="Image description" width="800" height="115"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For reference I have put the full app/root.tsx and app/routes/index.tsx below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5l1uya23atfkziiuvlxf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5l1uya23atfkziiuvlxf.png" alt="Image description" width="386" height="880"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;root.tsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import {
  Links,
  LiveReload,
  Meta,
  Outlet,
  Scripts,
  ScrollRestoration,
  useCatch,
  useLoaderData
} from "remix";
import type { MetaFunction } from "remix";
import { Box, ChakraProvider, CSSReset, Heading } from '@chakra-ui/react'
import theme from "~/theme";


export const meta: MetaFunction = () =&amp;gt; {
  return { title: "New Remix App" };
};

export default function App() {
  return (
    &amp;lt;Document&amp;gt;
     &amp;lt;ChakraProvider theme={theme}&amp;gt;
       &amp;lt;CSSReset/&amp;gt;
       &amp;lt;Outlet /&amp;gt;
     &amp;lt;/ChakraProvider&amp;gt;
    &amp;lt;/Document&amp;gt;
  );
}


function Document({
  children,
  title = "App title"
}: {
  children: React.ReactNode;
  title?: string;
}) {
  return (
    &amp;lt;html lang="en"&amp;gt;
      &amp;lt;head&amp;gt;
        &amp;lt;meta charSet="utf-8" /&amp;gt;
        &amp;lt;meta name="viewport" content="width=device-width,initial-scale=1" /&amp;gt;
        &amp;lt;Meta /&amp;gt;
        &amp;lt;title&amp;gt;{title}&amp;lt;/title&amp;gt;
        &amp;lt;Links /&amp;gt;
      &amp;lt;/head&amp;gt;
      &amp;lt;body&amp;gt;
        {children}
        &amp;lt;ScrollRestoration /&amp;gt;
        &amp;lt;Scripts /&amp;gt;
        {process.env.NODE_ENV === "development" &amp;amp;&amp;amp; &amp;lt;LiveReload /&amp;gt;}
      &amp;lt;/body&amp;gt;
    &amp;lt;/html&amp;gt;
  );
}

export function ErrorBoundary({ error }: { error: Error }) {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;Document title='Error!'&amp;gt;
      &amp;lt;ChakraProvider &amp;gt;
        &amp;lt;Box&amp;gt;
          &amp;lt;Heading as='h1'&amp;gt;There was an error&amp;lt;/Heading&amp;gt;
        &amp;lt;/Box&amp;gt;
      &amp;lt;/ChakraProvider&amp;gt;
    &amp;lt;/Document&amp;gt;
    &amp;lt;/div&amp;gt;

  )
}

export function CatchBoundary() {
  let caught = useCatch()

  return (
    &amp;lt;Document title={`${caught.status} ${caught.statusText}`}&amp;gt;
      &amp;lt;ChakraProvider&amp;gt;
        &amp;lt;Box&amp;gt;
          &amp;lt;Heading as='h1'&amp;gt;
            {caught.status} {caught.statusText}
          &amp;lt;/Heading&amp;gt;
        &amp;lt;/Box&amp;gt;
      &amp;lt;/ChakraProvider&amp;gt;
    &amp;lt;/Document&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;index.tsx&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import styles from "~/styles/index.css";
import {Links,LiveReload,Meta,Outlet,Scripts,ScrollRestoration,useCatch,useLoaderData} from "remix";
import { Box, Center, ChakraProvider, Circle, CSSReset, Heading, Text, ThemeProvider } from '@chakra-ui/react'


export const links = () =&amp;gt; {
  return [{ rel: "stylesheet", href: styles }];
};

export default function Index() {

  return (
    &amp;lt;&amp;gt;

      &amp;lt;div&amp;gt;
        &amp;lt;h1 className="raleway"&amp;gt;ABCDEFG Test of Font using className&amp;lt;/h1&amp;gt;
        &amp;lt;Heading&amp;gt;ABCDEFG Test of Font from Heading&amp;lt;/Heading&amp;gt;
        &amp;lt;Text&amp;gt;ABCDEFG Test of Font from Text&amp;lt;/Text&amp;gt;
      &amp;lt;/div&amp;gt;

    &amp;lt;/&amp;gt;

  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>remix</category>
      <category>chakraui</category>
      <category>font</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
