Testing different fonts in a React project - reactjs

Question
How to quickly test different fonts in a React project?
Overview
I'm learning how to use React/Gatsby with this template. In this example the site uses .sass files for styling and I see font-family: "slick" is referenced in the slider.sass file and reset.sass file has this entry:
body
font-family: Roboto, Helvetica, Arial, sans-serif
font-size: 16px
Desired outcome
I would ideally like to experiment as quickly as possible with lots of different combinations of fonts in this and other projects.
For example, I would like to try changing all fonts to something like this for titles/headers and this for everything else.
What have I looked at?
I've seen this from Gatsby founder kyleamathews but my guess is that it would clash with current sass configuration in this example.
I also see that variables can be used with sass and could potentially be used for testing different fonts in this project but I'm not sure exactly how.
Thanks for any help showing how I should approach this.

Let me kick my answer off with a warning:
Disclaimer: I do not recommend doing this in production. This is intended for testing font pairings locally.
Once you have chosen your fonts, I suggest hosting webfonts on your domain to avoid hitting a remote CDN. You could use classic #font-face declarations, or Kyle Matthew's typefaces packages, for example.
Now, what you basically want to do is to import fonts client-side, to make it easy to try them out without rebuilding your site.
1. Get a link to embed your fonts client-side
First, you'll need to get an embed link from your font hosting CDN (in your case, from Google Fonts). It will look like this:
https://fonts.googleapis.com/css2?family=Great+Vibes&family=Montserrat
2. Embed the fonts on your Gatsby site
To embed the link on your Gatsby site, you have two choices:
using <link>
You can add a font to your Gatsby app as an external client-side package. You would typically either customize html.js, or use react-helmet.
In your case, I see here that react-helmet already built into the starter you're using, so you would need to update layout.js like this:
<HelmetDatoCms
favicon={data.datoCmsSite.faviconMetaTags}
seo={data.datoCmsHome.seoMetaTags}
>
<link href="https://fonts.googleapis.com/css2?family=Great+Vibes&family=Montserrat&display=swap" rel="stylesheet">
</HelmetDatoCms>
Check out the README of gatsby-source-datocms to read more about the HelmetDatoCms component
using #import
You can import a remote font in CSS (or SASS):
#import url('https://fonts.googleapis.com/css2?family=Great+Vibes&family=Montserrat&display=swap');
This is already being used in your starter, the import is in reset.sass. You would simply need to update the URL with one that includes the fonts you want to try out.
In your case, I would recommend the second option. You'll only need to edit a single file, which will make testing several fonts faster.
3. Use the fonts in your CSS
Third, no matter if you chose the <link> or the #import option, you'll need to update your CSS to use the fonts you've imported. As you mentioned already in your question, this is where is happens.
You'll want something like this:
body {
font-family: 'Montserrat', sans-serif;
}
h1, h2, h3 {
font-family: 'Great Vibes', cursive;
}

Related

React Create Application production build and CSS variables in IE11

I would like to know how to get rid of CSS variables in a production build. The fact is that I need to build a project for IE11 in which CSS variables are not supported. My project was created using React Create App and I can't do npm run eject. How can you set up a production build so that in the initial heart rate of the style, instead of variables, there would be their values?
Don't offer css-vars-ponyfill, it takes a very long time to load a page in IE11.
The only way to make CSS variables work in IE is using a polyfill. If you don't want to use css-vars-ponyfill, you can try ie11CustomProperties or css-variables-polyfill.
If you don't want to use a polyfill, I think you can only edit the CSS styles manually to add CSS fallbacks to support IE11. Something like this:
body {
--text-color: red;
}
body {
color: red;
color: var(--text-color);
}

Custom font in Electron + React application not working after running the Windows installer

I'm building an Electron app that needs to run on Windows. The stack exists of React + Typescript, SCSS + TailwindCSS. The style is compiled with postcss. The Electron app is built with electron-builder.
After compiling the style, creating new Electron installer for Windows and running this installer the included font's don't seem to work.
When checking the build folder that functions as the output folder for electron-builder, all font files are present in the static/media folder. If I check the link to the font files when the Electron app is running, the link in the CSS also links to the generated static folder where the files are.
Files & code
The font files are located in: src/assets/font/;
For each weight I use (only 2) there are: .ttf, .woff, woff2, .svg and .eot available;
The font is loaded through SASS:
#font-face {
font-family: 'Montserrat';
font-weight: 500;
src: url('../font/Montserrat-Medium.ttf') format('truetype'),
url('../font/Montserrat-Medium.woff2') format('woff2'),
url('../font/Montserrat-Medium.woff') format('woff'),
url('../font/Montserrat-Medium.svg') format('svg'),
url('../font/Montserrat-Medium.eot') format('eot');
}
#font-face {
font-family: 'Montserrat';
font-weight: 700;
src: url('../font/Montserrat-Bold.ttf') format('truetype')
url('../font/Montserrat-Bold.woff2') format('woff2'),
url('../font/Montserrat-Bold.woff') format('woff'),
url('../font/Montserrat-Bold.svg') format('svg'),
url('../font/Montserrat-Bold.eot') format('eot');
}
Then it is loaded in TailwindCSS as basic font, configured in the tailwindconfig:
theme: {
fontFamily: {
sans: ['Montserrat', 'sans-serif'],
body: ['Montserrat'],
bold: ['Montserrat']
},
... rest of config file
Facts
The font works if it is installed locally on Windows.
When it's not found, the app doesn't fallback to a sans-serif font.
The font files and link to the font files is correct when checking the CSS in the running application (Chromium element inspector).
All other styles work correctly, only the font isn't working. So it looks like the CSS is compiled and loaded properly.
Checked resource
Custom font is not showing on Electron + Vue App (yes it's Vue, but the answer still is: use #font-face).
https://www.reddit.com/r/electronjs/comments/bhaiva/how_to_load_custom_fonts_in_electron_app/
https://discuss.atom.io/t/how-to-use-specific-font-in-electron-app/32215
https://github.com/electron-userland/electron-webpack/issues/163
https://dev.to/theola/electron-app-with-webpack-bootstrap-fontawesome-and-jquery-a-complete-guide-54k2
Questions
When running the installer.exe that is created with electron-builder, do you need to include an assets folder and/or other files in the installation folder (e.g.: builder-effective-config.yaml)?
Are there other ways to include a local font? I don't want to use a CDN or other online source.
All resources I currently found say it should work when using #font-face.
Note: if you require more information, please let me know and I'll provide it.
Ok, after a little more testing/checking the issue was the React build. It loaded an older configuration, that was breaking the font load.
Thanks all who read it!

CSS Styling with react-dates

Hopefully a simple question with a simple answer, but how exactly do you use custom CSS to style react-date components? The docs on Github are about as clear as mud to me when it comes to CSS and seems to push users to use the theme method...which seems like a perfect way to make my project even more messy and confusing. I understand that I have to import react-dates/initialize and react-dates/lib/css/_datepicker.css, but importing my own CSS does not seem to affect the styling. I am using the default create-react-app setup for my application.
First you will need to include the libraries CSS and then include your custom CSS to override the libraries CSS.
require("libraries/style.css"); // Make sure to include this before your custom css
require("custom/style.css");
In your custom style.css file, you will need to override the styling rules from style.css like so.
.library-css-rule {
color: #000;
}

Load different css on different pages

I'm trying to find a solution to this problem:
I'm using a template with different css includes based on page, ex:
Login uses login.css
Home uses home.css
If I load both css the login page is broken, because styles are overwritten by home.css
So I need to load or require login.css if the route or the component is Login and the other one when is Home.
If I load both webpack builds a global css with both files, and everything is broken...
I tried to require the css in componentDidMount, but I think that is not the way :)
Thanks in advance
It sounds like both these styles are quite specific to the pages, so why not simply namespace them?
Within your templates, have a .login/.home class, and use this as the namespace within the css. If you're using sass, this is as simple as wrapping all the sass in the class. Otherwise, you can go through and add the class to the beginning of all the elements/clases.
First of all, you shouldn't have any problems if you use different css classes for your views and just style the elements based on those classes.
The best way to load css in react is to do it by components, if you got a component login.jsx, in your styles folder (or whatever folder you're using to hold your styles) create a sass partial _login.scss and add the css selectors and styles for that given component, and do that for every component in your react application.
Then you just include those partials into a main.scss file and that's the file you want to load into your react app.
Here's an example of a main.scss file with some sass partials.
#import 'base/variables';
#import 'base/defaults';
#import 'components/login';
#import 'components/home';
That's a good and clean way to work with styles in react, of course you will need to configure your webpack in order to get sass to work in your application.
Take a look at this and this for more info.
This is a more generic approach to combine CSS files, without depending on technologies like SASS or reactjs.
I assume, if you combine the two CSS files, you are using Grunt or similar tool, to automate that task. So automatically updating the CSS files should be OK for you, even though they are from an external source and you want to use updated versions without making manual changes.
I also assume, you are using classes to style your pages, so there are no tag based styles in your CSS. Because you cannot rename the tags in the CSS file without braking it or make larger changes to your code.
If my assumptions are true, you could use something like grunt-css-prefix. It can add prefixes to your CSS classes for the login page, like in this snippet.
Original CSS file content:
.foo,
.bar,
h1 {
display: none;
}
CSS file content after running the Grunt script:
.login-foo,
.login-bar,
h1 {
display: none;
}
Just use the login-foo like class names in your Login-HTML and you are good to go.
For more details on how to use grunt-css-prefix, please have a look at https://www.npmjs.com/package/grunt-css-prefix.

Ionic - Using Javascript to define SASS variables

I am making a hybrid ionic phonegap app.I am using sass to define color variables.
As part of my app I need to be able to change the colors depending on what is defined in an external database.
I know how to connect to the server in my controllers to return Json data.
Is there anyway I could connect to the server using HTTP requests in my sass file?
So that it is possible for the app colors to change depending on what is saved in the database?
Any help is much appreciated.
Short answer is no.
You should be converting your SCSS to CSS so you have whatever you set.
A way that you can achieve something similar is by utilising SCSS themes. How this works is to simply overwrite all your colour vars with different themes.
$black: #000;
$white: #fff;
$red: #e2061c;
$gray-light: #c9c8c8;
$gray: #838282;
$gray-dark: #333333;
$blue: #253652;
You could then use angular (or whatever framework you are using) to switch out the style sheet depending on which theme your users have chosen.
Angular -
<link rel="stylesheet" type="text/css" ng-href="{{user.theme}}.css">
SCSS -
Folder structure
|- _scss/
|-|- _base/
|-|-|- _config.scss
|-|- _layouts/
|-|-|- _l-grid.scss
|-|-|- _l-default.scss
|-|- _modules/
|-|-|- _m-accordions.scss
|-|-|- _m-teasers.scss
|-|-_themes/
|-|-|- _light-theme/
|-|-|-|- light.scss
|-|- application.scss
This atrical goes into great detail on how to theme your applications

Resources