Alternative to createGlobalStyle from styled-components that can also be importable - reactjs

we are currently loading Fonts and few other global styles like this:
import { createGlobalStyle } from 'styled-components';
export default createGlobalStyle`
#font-face {
font-family: 'Name';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(https://fonts.gstatic.com/s/name/v12/iJWZBXyIfDnIV5PNhY1KTN7Z-Yh-B4iFU0U1Z4Y.woff2) format('woff2');
}
// more fonts..
}
And in every _app.tsx (from every project in repo) we just
import GlobalStyle from #our-company/ui;
// few other imports
const AppProviders = ({ children, messages, locale }: Props): JSX.Element => {
return (
<IntlProvider
locale={locale || 'en-GB'}
key={locale}
messages={messages[locale]}
defaultLocale="en-GB"
>
<GlobalStyle />
<DsThemeProvider
locale={locale}
>
{children}
</DsThemeProvider>
</IntlProvider>
);
};
But we noticed unnecessary font reloads caused by this GlobalStyle when clicking, for example, in checkbox elements (tried putting this in a .css and just load it and never happens again).
Any idea how could export this styles as GlobalStyle name without using styled-components so we don't have to change all import from all apps in the project?

why you dont create main.css and import it on your index.css or app.css , its download and cache on users browser so you don't need to use global styled component anymore

It seems like you're looking for something like injectGlobal:
import { injectGlobal } from 'styled-components';
injectGlobal`
/* your #font-face stuff here */
`
This seems like it would be a good fit for your situation, as it would be relatively easy to transition from the existing structure using createGlobalStyle to one that uses this.
In your case, the code would look like:
import { injectGlobal } from 'styled-components';
injectGlobal`
#font-face {
font-family: 'Name';
font-style: normal;
font-weight: 400;
font-display: swap;
src: url(https://fonts.gstatic.com/s/name/v12/iJWZBXyIfDnIV5PNhY1KTN7Z-Yh-B4iFU0U1Z4Y.woff2) format('woff2');
}
// more fonts..
}
If you wish to use a solution that is backwards-compatible with any code that is rendering <GlobalStyle />, you can include a null component as the default export as a stop-gap solution, alongside your injectGlobal code:
export default () => null;
Would this solve your problem?

You can also bind the style directly inside the JS file
var stylingObject = {
div: {
color: "red",
border: "1px solid red"
}, input: {
margin: "2px",
padding: "5px"
}
}
function App() {
return (
<div style={stylingObject.div}>
<input style={stylingObject.input} type="text" />
</div>
);
}

Originally I had hoped to import some CSS file with the fonts defined there and then just importing it so it would affect the entire page, obviously because the way react works such a thing isn't possible cause it would take place only in the imported component not in the one that did the import.
the reason createGlobalStyle from styled-components renders each time is also due to react's workings - when we set the style for a "component like" (or actual component) object, like every react component its not static and renders only when needed. Even if we make it render by force on page load its not the same one (cause on each page we render it separately) so keeping up with the current config doesn't seem to be possible by react's standard.
If we want to make the styles static or "more" static we would have to :
either import them in each page separately - making us do a major refactor
or use either createGlobalStyle (allowing us to use our already created styled-components component) or importing a main css file in the main application component like in the example below:
globalStyles.js
import { createGlobalStyle } from 'styled-components';
const GlobalStyle = createGlobalStyle`
body {
margin: 0;
padding: 0;
background: teal;
font-family: Open-Sans, Helvetica, Sans-Serif;
}
`;
export default GlobalStyle;
App.js
import React, { Fragment } from 'react';
import GlobalStyle from './theme/globalStyle';
import Content from './components/Content';
function App() {
return (
<Fragment>
<GlobalStyle />
<Content />
</Fragment>
);
}
export default App;

If you have a rather large global stylesheet like we did while migrating, you can use styled-components css method to leverage styled-components (css) IDE syntax highlighting & linting you can also do the following:
import React from 'react'
import { createGlobalStyle, css } from 'styled-components'
const Reset = css`
* {
box-sizing: border-box;
}
`
const Accessibility = css`
.hidden {
display: none !important;
visibility: hidden;
}
`
const BaseStyles = createGlobalStyle`
${Reset};
${Accessibility};
`
export const GlobalStyles = () => (
<>
<BaseStyles />
</>
)
Import GlobalStyles and render as sibling to {children}

Related

NextJS with styled-components fast refresh not working

I have a NextJS app being used with styled-components.
I have these 3 files:
Worth noting that some markups are removed for clarity sake so only the related codes are pasted.
Header.js
import {
HeaderContainer,
SearchInput,
SearchWrapper
} from './HeaderStyles';
import { Input } from '../GlobalComponents/GlobalComponents';
const Header = () => {
return (
<HeaderContainer>
<SearchWrapper>
<SearchInput type='text' placeholder='Search movie' />
</SearchWrapper>
</HeaderContainer>
);
}
export default Header;
HeaderStyles.js
import styled from 'styled-components';
import { Input } from '../GlobalComponents/GlobalComponents';
export const HeaderContainer = styled.header`
background-color: ${props => props.theme.colors.primary};
display: flex;
align-items: center;
box-sizing: border-box;
`;
export const SearchWrapper = styled.div`
flex-grow: 3;
background-color: red;
`;
export const SearchInput = styled(Input)`
background-color: yellowgreen;
`;
GlobalComponents.js
import styled from "styled-components";
export const Input = styled.input`
padding: 1rem;
`;
Attached is my
Project Structure
Note that inside HeaderStyles.js, the SearchInput is extended from Input in GlobalComponents.js
Whenever I change css properties in HeaderStyles.js, the fast refresh works just fine. However, in the case of GlobalComponents.js, I had to manually reload the page to view the changes.
If I were to put my generic Input styling into HeaderStyles, it works fine, but that isn't how I wanted to structure it. So I guess it somewhat related to the imported modules not being in the React tree or stuff like that.
I have been looking for solutions online but got no luck. Would like to know the causes and solution for this. Thanks in adv.
I think your problem about styled-component at SSR.
You can try change your pages/_document.js and your babel config.
add this codes to .babelrc
{
"presets": ["next/babel"],
"plugins": [["styled-components", { "ssr": true }]]
}
_document.js
https://github.com/vercel/next.js/blob/main/examples/with-styled-components/pages/_document.js
with the Next Complier on new version of next, you should only update your next.config file and _document file, and you will be all set. Babel will cause conflict with the NextJS compiler.
https://github.com/vercel/next.js/tree/canary/examples/with-styled-components
Make sure you have pages/_document.js with codes stated here.

why styled component styling not adding

i was scratching my head from the last 2 hours that why this 3 rd party styled component is not adding i it .have a look
import React from 'react';
import styled from 'styled-components'
const StyleDiv =styled.div`
#media(max-width:600px){
body{
background-color: red;
}
}`
const Person= (props)=>{
return <StyleDiv>
<h1 onClick={props.click}>i am {props.name} and {props.age} years old as you know me as {props.children} </h1>
<input type="text" onChange={props.changed} />
</StyleDiv>
}
export default Person;
If you put selectors within the template string without the ampersand, they will refer to children of the component. https://styled-components.com/docs/basics#pseudoelements-pseudoselectors-and-nesting
Basically you can't select body within your div and you probably shouldn't.
you cannot apply global styles like this to a component, for applying global styles (i.e. styles to body, html etc.) styled-components gives a createGlobalStyles utility, it can be used like this:
import { createGlobalStyles } from 'styled-components';
const GlobalStyles = createGlobalStyle`
#media (max-width: 600px) {
body {
background-color: red;
}
}
`;
// Render GlobalStyles to the root lvvel of your app
ReactDOM.render(
<>
<GlobalStyles />
<App />
</>,
document.getElementById('root');
);
Hope it helps :)

Flash Of Unstyled Text (FOUT) on reload using next.js and styled components

I'm using global style from styled components with next.js and every time I reload my page I can see the font flickering.
I have my font files in public/fonts/Inconsolata
I've looked everywhere in spectrum chat, next.js github issues but can't seem to find any solution.
pages/index.js
import styled from 'styled-components';
const H1 = styled.h1`
font-family: 'Inconsolata Bold';
font-weight: 700;
color: #000;
`;
const index = () => {
return (
<div>
<H1>font flashes</H1>
</div>
);
};
export default index;
pages/_app.js
import App from 'next/app';
import React from 'react';
import GlobalStyle from '../src/style/globalStyle';
export default class MyApp extends App {
render() {
const { Component, pageProps } = this.props;
return (
<>
<GlobalStyle />
<Component {...pageProps} />
</>
);
}
}
pages/_document.js
import Document from 'next/document';
import { ServerStyleSheet } from 'styled-components';
export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props => sheet.collectStyles(<App {...props} />)
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
)
};
} finally {
sheet.seal();
}
}
}
style/globalStyle.js
import { createGlobalStyle } from 'styled-components';
const globalStyle = createGlobalStyle`
#font-face {
font-family: 'Inconsolata';
src: url('./fonts/Inconsolata/Inconsolata-Regular.woff2') format('woff2'),
url('./fonts/Inconsolata/Inconsolata-Regular.woff') format('woff');
font-weight: 400;
font-style: normal;
font-display: fallback;
}
#font-face {
font-family: 'Inconsolata Bold';
src: url('./fonts/Inconsolata/Inconsolata-Bold.woff2') format('woff2'),
url('./fonts/Inconsolata/Inconsolata-Bold.woff') format('woff');
font-weight: 700;
font-style: bold;
font-display: fallback;
}
`;
export default globalStyle;
UPDATE:
Next.js released a new feature called Automatic Webfont Optimization.
Just include your font (it works only with Google Fonts so far) and it will be included as raw css on build-time.
// Before
<link
href="https://fonts.googleapis.com/css2?family=Inter"
rel="stylesheet"
/>
// After
<style data-href="https://fonts.googleapis.com/css2?family=Inter">
#font-face{font-family:'Inter';font-style:normal.....
</style>
Check out how Next.js guys handle it on their website, which is open-source and can be found here. Check it out, it worked for me.
Import your used font in css #font-face via preload link
<link
rel="preload"
href="/assets/my-font.woff2"
as="font"
type="font/woff2"
/>
Your font declaration should be on SSR HTML page, so use <style jsx global /> to include it in your page. It can be an external file or right directly in style element.
I had a similar problem with NextJS 12, Google Fonts and SCSS modules.
My partial solution was to
'preload' the resources being requested in any #font-face rules in any CSS file - load the fonts more eagerly
set font-display: optional - tell CSS to use fallback if font not loaded in time
This means no Flash Of Unstyled Text (FOUT) but on slower connections a fallback font will be used on first load instead.
<head>
<link rel="preconnect" href="https://fonts.googleapis.com" />
<link
rel="preload"
href="https://fonts.gstatic.com/s/inconsolata/v21/QlddNThLqRwH-OJ1UHjlKENVzkWGVkL3GZQmAwLyya15IDhunA.woff2"
as="font"
type="font/woff2"
crossOrigin=""
/>
<link
rel="preload"
href="https://fonts.gstatic.com/s/inconsolata/v21/QlddNThLqRwH-OJ1UHjlKENVzkWGVkL3GZQmAwLyx615IDhunJ_o.woff2"
as="font"
type="font/woff2"
crossOrigin=""
/>
// CSS file with #font-face rules and font-display: optional
<link
href="https://fonts.googleapis.com/css2?family=Inconsolata:wght#300;400;700&display=optional"
rel="stylesheet"
/>
</head>
In this example, the last link element requests a CSS file which has a number of #font-face rules with src declarations and url() values.
If the resources in the url() functions aren't preloaded in the head then they wont be requested until the CSS is parsed.
I think this is what causes the FOUT.
By including <link rel="preload" src="..."/> elements fonts can be loaded sooner.
And setting font-display: optional tells the browser that if the font hasn't loaded in time to use a fallback.
The site I was working on: Soundboard
Create an style.css file in your public/fonts/ directory. Copy and paste your #font-face part in style.css.
style.css
#font-face {
font-family: "Inconsolata";
src: url("./fonts/Inconsolata/Inconsolata-Regular.woff2") format("woff2"),
url("./fonts/Inconsolata/Inconsolata-Regular.woff") format("woff");
font-weight: 400;
font-style: normal;
font-display: fallback;
}
#font-face {
font-family: "Inconsolata Bold";
src: url("./fonts/Inconsolata/Inconsolata-Bold.woff2") format("woff2"),
url("./fonts/Inconsolata/Inconsolata-Bold.woff") format("woff");
font-weight: 700;
font-style: bold;
font-display: fallback;
}
In your pages/_app.js import the style.css file in head.
_app.js
import App from 'next/app';
import React from 'react';
import Head from "next/head";
import GlobalStyle from '../src/style/globalStyle';
export default class MyApp extends App {
render() {
const { Component, pageProps } = this.props;
return (
<>
<GlobalStyle />
<Head>
<link rel="stylesheet" href="/fonts/style.css" />
</Head>
<Component {...pageProps} />
</>
);
}
}
Hat tip to Raul Sanchez on dev.to for the answer to this one:
Next doesn't fetch styled-components styles on the server, to do that you need to add this page to pages/_document.js:
import Document from 'next/document'
import { ServerStyleSheet } from 'styled-components'
export default class MyDocument extends Document {
static async getInitialProps(ctx) {
const sheet = new ServerStyleSheet()
const originalRenderPage = ctx.renderPage
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: (App) => (props) =>
sheet.collectStyles(<App {...props} />),
})
const initialProps = await Document.getInitialProps(ctx)
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
}
} finally {
sheet.seal()
}
}
}
This code may update, so check Next's styled-components example for the latest.
I had the same problem and after hours of experimenting with different methods npm package fontfaceobserver solved the problem for me.
With the package you can tell your app to render only after fonts are loaded, thus avoiding FOUT like so:
import FontFaceObserver from "fontfaceobserver";
const font = new FontFaceObserver("Inconsolata");
font.load().then(()=> {
ReactDOM.render(<App />,document.getElementById("root"));
}

Merge classes in CSS modules using React, SASS and webpack

I am trying to create a simple way to override CSS classes in React components that use CSS Modules.
I can see the stringified names of the classes being imported from styles and being passed in via theme, but I can't seem to figure out how to see the actual CSS properties.
Is there a way to get access to these and merge them into a new class which can then be applied via className?
// component1.scss
.base {
background-color: black; <-- override this property
}
// component1.js
import Component2 from './component2';
import styles from './component1.scss';
const theme = {
base: styles.base
};
function Component1() {
return <Component2 theme={theme} />;
}
// component2.scss
.base {
background-color: yellow;
text-align: center;
ul {
margin: 10px;
}
}
// component2.js
import Component2 from './component2';
import styles from './component2.scss';
function Component2({ theme }) {
return (
<div className={() => someFunctionThatMergesCss(styles, theme)}>
<ul>
...
</ul>
</div>
);
}

How can I setup SCSS files (ViewEncapsulated way) in 'react app 2' like Angular component specific SCSS?

I installed 'react app 2' as well as node-sass. It's working fine with SCSS. But I just want to know how can I create component specific SCSS like Angular (that will never be a conflict with other components SCSS)
Angular automatically add an attribute for ViewEncapsulation see below example
In angular, there is an option for
encapsulation: ViewEncapsulation.None (Use to disable CSS Encapsulation for this component)
enter link description here
I know the question is old, but it has no answer, thus I want to share this article. Alsomst does the trick, with the exception that it seems to does not have support for something like ::ng-deep
React doesn't have native component styles like Angular does because it aims to keep away from any functionality that could easily be handled by third-party packages. So you have two pretty simple options:
Use styled-components to create component-specific styles. This is a pretty straightforward package that allows you to define styles for each element within a component and you can even pass variables into the styles. It generates internal CSS (kept in <style> tags in the document head) which will take precedence over external styles by default. Example:
// MainComponent.jsx
import React from 'react';
import styled from 'styled-components';
const Title = styled.h1`
color: red
`
const MainComponent = (props) => <Title>Hello World</Title>
In each of your components, add a class or ID to the root element so that you can simply add that selector to the beginning of your SCSS to only style that specific component. Example:
// MainComponent.jsx
import React from 'react';
const MainComponent = (props) => (
<div className="main-component">
<h1>Hello World</h1>
</div>
)
// MainComponent.scss
.main-component {
h1 {
color: red;
}
}
Now only h1 elements in your MainComponent will be red.
//JS
import React from "react";
import "./yourComponentName.scss";
export default props => {
const { className, children, ...restOperator } = props;
return (
<a className={`yourComponentName ${className}` } {...restOperator}>
{children}
</a>
);
}
//yourComponentName.scss
.yourComponentName{
position:relative;
background:red;
/* your property and value use nesting*/
ul {
margin: 0;
padding: 0;
list-style: none;
}
li { display: inline-block; }
a {
display: block;
padding: 6px 12px;
text-decoration: none;
}
}

Resources