React - Electron, using React-Quill package with styles not applying to component - css

I have this very weird problem with the React-Quill library component.
I am importing the component as per the NPM instructions, and the editor does load but without any actual CSS applied
The only when the styles are applied are when I link the stylesheet through CDN in the index.html file
<link rel=“stylesheet” href=“//cdn.quilljs.com/1.2.6/quill.snow.css”>
I’ve tested in a stackblitz the same code and it works without such issues.
I am using similar other components such as ‘react-color-palette’ where I do import the styles in the same fashion through the node_modules and there is no problem with it, so it doesn’t make sense to me to be a webpack config problem either but I tested various options with different settings on webpack to no avail.
Even added the CSS to my global CSS which works perfectly fine too, but it still doesn’t apply to the React-Quill
For some reason, the CSS isn't targeting the classes, or its scoped out, but I am unable to understand why that happens
Here is the code
import React, { useState } from 'react';
import hljs from 'highlight.js';
import ReactQuill from 'react-quill';
import 'react-quill/dist/quill.snow.css';
const modules = {
// syntax: {
// highlight: (text: string) => hljs.highlightAuto(text).value,
// },
toolbar: [
[{ header: [1, 2, 3, 4, 5, 6, false] }],
['bold', 'italic', 'underline', 'strike'],
[{ color: [] }],
[{ align: [] }],
[{ indent: '-1' }, { indent: '+1' }],
[{ list: 'ordered' }, { list: 'bullet' }],
['link', 'image', 'video'],
['code-block', 'blockquote'],
[{ script: 'sub' }, { script: 'super' }],
],
clipboard: {
matchVisual: false,
},
syntax: false,
};
const textFormats = [
'font',
'header',
'size',
'bold',
'italic',
'underline',
'strike',
'blockquote',
'background',
'color',
'script',
'list',
'bullet',
'link',
'image',
'video',
'clean',
'code-block',
'indent',
'list',
'align',
];
function RichTextEditor() {
const [value, setValue] = useState('');
hljs.configure({
// useBR: false,
languages: [
'javascript',
'java',
'html',
'xml',
'sql',
'typescript',
'bash',
],
});
return (
<ReactQuill
value={value}
onChange={setValue}
placeholder="Write a new Note"
modules={modules}
formats={textFormats}
/>
);
}
export default RichTextEditor;
Edit
I opened the console and checked in the tags
there are styles currently applied for the react-quill, but seems react modifies the class names:
.RichTextEditor__ql-container__SP9Yv
Where it should really just be
.ql-container

Related

Nuxt 3 + Vuetify 3 how to plug custom SASS variables

I'm struggling to get my setup to work using
"vuetify": "^3.1.4"
"nuxt": "^3.1.2"
"vite-plugin-vuetify": "^1.0.2",
"#mdi/font": "^7.1.96",
"sass": "^1.58.0",
I've read as many ressources as possible on the topic, starting with this one which seems outdated?
Here's my setup:
// assets/styles/config.scss
#use "vuetify/settings" with (
$font-size-root: 20px,
$heading-font-family: Space Grotesk,
$body-font-family: $font,
$color-pack: false,
$utilities: false,
$typography: (
"h1": (
"size": 2rem,
"weight": 500,
"line-height": 2.2rem,
"letter-spacing": 0,
"font-family": Comic Sans,
"text-transform": none,
),
)
);
import "#/assets/styles/config.scss";
import { createVuetify } from "vuetify";
export default defineNuxtPlugin((nuxtApp) => {
nuxtApp.vueApp.use(
createVuetify({
ssr: true,
// components,
// directives,
styles: { configFile: "assets/styles/config.scss" },
theme: {
defaultTheme: "light",
themes: {
dark: false,
light: {
colors: {
primary: "#4093ff",
secondary: "#4d4d4d",
success: "#25c760",
warning: "#ffc82e",
error: "#ff4c29",
},
},
},
},
})
);
});
import vuetify from "vite-plugin-vuetify";
// https://nuxt.com/docs/api/configuration/nuxt-config
export default defineNuxtConfig({
css: ["vuetify/lib/styles/main.sass", "#mdi/font/css/materialdesignicons.min.css", "#/assets/styles/config.scss"],
build: {
transpile: ["vuetify"],
},
modules: [
// #ts-ignore
async (options, nuxt) => {
nuxt.hooks.hook("vite:extendConfig", (config) => config.plugins.push(vuetify()));
},
],
vite: {
ssr: {
noExternal: ["vuetify"], // add the vuetify vite plugin
},
},
});
Custom theme will work but SASS variables are ignored.
Any help appreciated!
Thanks!!
After a full day of researches, I found this github issue, which includes a temporary fix of the underlying bug:
https://github.com/nuxt/nuxt/issues/15412#issuecomment-1399189084
I hope others won't have as much trouble as I did!

Rollup plugin scss doesn't embed/import css in final index.js

I' ve got this simple library:
src
L component
L Player.tsx
PlayerUI.style.scss
index.ts
Player.tsx import the scss:
import './PlayerUI.style.scss';
with this roll-up config:
const packageJson = require("./package.json");
export default [
{
input: "src/index.ts",
output: [
// {
// file: packageJson.main,
// format: "cjs",
// sourcemap: true,
// },
{
file: packageJson.module,
format: "esm",
sourcemap: true,
},
],
plugins: [
del({ targets: 'dist/*' }),
resolve(),
commonjs(),
typescript({ tsconfig: "./tsconfig.json" }),
json(),
scss(),
livereload(),
],
external: ['react-dom', 'axios', 'react-redux', 'react', '#reduxjs/toolkit', 'react-audio-player'],
},
{
input: 'src/components/player-ui/PlayerUI.style.scss',
output: [
{ file: "dist/esm/components/player-ui/PlayerUI.style.scss" }
],
plugins: [
scss()
],
},
{
input: "dist/esm/index.d.ts",
output: [{ file: "dist/index.d.ts", format: "esm" }],
plugins: [dts(),]
},
];
I get this:
dist
L esm
L components
Player.d.ts
PlayerUI.style.scss
**index.css**
**index.js**
index.d.ts
The library itself works pretty well, but I get no css style in browser.
I checked the index.js file, and there is no import instructions, or stringified css in it. Basically, the index.css file is isolated, no one calls/imports it.
What am I missing?

issue loading css module classes in storybook v6.4

I'm having trouble getting storybook to play nice with css modules within my Gatsby project. I'm able to render the button component but it does not add any of my styling. On inspection of the element, I'm only getting undefined undefined from the following code.
button.jsx
import React from "react"
import * as css from "./style.module.css"
const Button = ({ variant = "button", type, value = null }) => {
const baseOfVariant = () => {
if (variant === "input") {
return (
<input
type={type}
value={value}
className={`${css.button} ${css.clear_button}`}
/>
)
}
return (
<button type={type} className={`${css.button} ${css.submit_button}`}>
{value}
</button>
)
}
return baseOfVariant()
}
export default Button
button.stories.jsx
import React from "react"
import Button from "./button"
export default {
title: "Button",
component: Button,
}
export const Template = args => <Button {...args} />
export const ButtonRegular = Template.bind({})
ButtonRegular.args = {
variant: "button",
value: "Click Me",
type: "submit",
}
main.js
module.exports = {
stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.#(js|jsx|ts|tsx)"],
addons: ["#storybook/addon-links", "#storybook/addon-essentials"],
core: {
builder: "webpack5",
},
}
Storybook stuff in my devDeps
"devDependencies": {
"#babel/core": "^7.14.6",
"#babel/polyfill": "^7.12.1",
"#storybook/addon-actions": "^6.4.0-alpha.2",
"#storybook/addon-essentials": "^6.4.0-alpha.2",
"#storybook/addon-links": "^6.4.0-alpha.2",
"#storybook/addon-viewport": "^6.4.0-alpha.2",
"#storybook/builder-webpack5": "^6.4.0-alpha.2",
"#storybook/manager-webpack5": "^6.4.0-alpha.2",
"#storybook/react": "^6.4.0-alpha.2",
"babel-loader": "^8.2.2",
"prettier": "2.2.1",
"resize-observer-polyfill": "^1.5.1"
}
Gatsby needs to import css modules with the following synthax:
import * as css from "./style.module.css"
Where Storybook recognizes only this synthax:
import css from "./style.module.css"
This is due to the fact that Gatsby and Storybook don't use the same import convention. Fortunately, you can configure Storybook css module import mechanism via .storybook/main.js file.
const path = require("path")
module.exports = {
// You will want to change this to wherever your Stories will live
stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.#(js|jsx|ts|tsx)"],
addons: ["#storybook/addon-links", "#storybook/addon-essentials"],
core: {
builder: "webpack5",
},
webpackFinal: async config => {
// Prevent webpack from using Storybook CSS rules to process CSS modules
config.module.rules.find(
rule => rule.test.toString() === "/\\.css$/"
).exclude = /\.module\.css$/
// Tell webpack what to do with CSS modules
config.module.rules.push({
test: /\.module\.css$/,
include: path.resolve(__dirname, "../src"),
use: [
{
loader: "style-loader",
options: {
modules: {
namedExport: true,
},
},
},
{
loader: "css-loader",
options: {
importLoaders: 1,
modules: {
namedExport: true,
},
},
},
],
})
// Transpile Gatsby module because Gatsby includes un-transpiled ES6 code.
config.module.rules[0].exclude = [/node_modules\/(?!(gatsby)\/)/]
// use babel-plugin-remove-graphql-queries to remove static queries from components when rendering in storybook
config.module.rules[0].use[0].options.plugins.push(
require.resolve("babel-plugin-remove-graphql-queries")
)
return config
},
}
With the above configuration, Storybook now accept this import synthax and button.jsx is correctly styled.
import * as css from "./style.module.css"
If any one is looking for sass/scss related fix
import * as style from "./style.module.scss"
Where Storybook recognizes only this synthax:
your .storybook/main.js file.
const path = require("path");
module.exports = {
stories: ["../src/**/*.stories.{js,mdx}"],
addons: [
"#storybook/addon-docs",
"#storybook/addon-actions",
"#storybook/addon-controls",
"#storybook/addon-a11y",
"#storybook/addon-viewport",
],
// https://gist.github.com/shilman/8856ea1786dcd247139b47b270912324
core: {
builder: "webpack5",
},
webpackFinal: async config => {
// https://www.gatsbyjs.com/docs/how-to/testing/visual-testing-with-storybook/
config.module.rules.push({
test: /\.(js)$/,
use: [
{
loader: require.resolve("babel-loader"),
options: {
presets: [
// use #babel/preset-react for JSX and env (instead of staged presets)
require.resolve("#babel/preset-react"),
require.resolve("#babel/preset-env"),
],
plugins: [
// use #babel/plugin-proposal-class-properties for class arrow functions
require.resolve("#babel/plugin-proposal-class-properties"),
// use babel-plugin-remove-graphql-queries to remove static queries from components when rendering in storybook
require.resolve("babel-plugin-remove-graphql-queries"),
// use babel-plugin-react-docgen to ensure PropTables still appear
require.resolve("babel-plugin-react-docgen"),
],
},
},
],
exclude: [/node_modules\/(?!(gatsby)\/)/],
});
config.module.rules.push({
test: /\.s[ac]ss$/i,
oneOf: [
// module.scss files (e.g component styles.module.scss)
// https://webpack.js.org/loaders/style-loader/#modules
{
test: /\.module\.s?css$/,
use: [
// Add exports of a module as style to DOM
{
loader: "style-loader",
options: {
esModule: true,
modules: {
namedExport: true,
},
},
},
// Loads CSS file with resolved imports and returns CSS code
{
loader: "css-loader",
options: {
esModule: true,
modules: {
namedExport: true,
},
},
},
// Loads and compiles a SASS/SCSS file
{
loader: "sass-loader",
// only if you are using additional global variable
options: {
additionalData: "#import 'src/styles/global.scss';",
sassOptions: {
includePaths: ["src/styles"],
},
},
},
],
},
// scss files that are not modules (e.g. custom.scss)
{
use: [
// Add exports of a module as style to DOM
"style-loader",
// Loads CSS file with resolved imports and returns CSS code
"css-loader",
// Loads and compiles a SASS/SCSS file
{
loader: "sass-loader",
// only if you are using additional global variable
options: {
additionalData: "#import 'src/styles/global.scss';",
sassOptions: {
includePaths: ["src/styles"],
},
},
},
],
},
],
});
return config;
},
};
With the above configuration, Storybook now accept this import synthax and button.jsx is correctly styled.
import * as styles from "./style.module.scss"

tailwindcss: add font-face in plugin

I try to include some font faces into a tailwind-preset I want to use across multiple projects.
In the documentation it is only described how to define font-faces within the base layer of a css file, see here: https://tailwindcss.com/docs/adding-base-styles#font-face-rules.
However to include it in a distributable preset the way to go (from my knowledge), is to include it in a plugin. However I do not get what is the problem.
My approach is the following:
const defaultTheme = require('tailwindcss/defaultTheme')
const plugin = require('tailwindcss/plugin')
module.exports = {
prefix: '',
mode: 'jit',
purge: {
content: [
'./src/**/*.{html,ts,css,scss,sass,less,styl}',
]
},
darkMode: false, // or 'media' or 'class'
theme: {
extend: {
fontFamily: {
sans: ['myfont', ...defaultTheme.fontFamily.sans]
},
}
},
variants: {
extend: {},
},
plugins: [ plugin(function ({ addBase}) {
const fonts = {
'#font-face': [
{
fontFamily: 'My Font',
fontStyle: 'normal',
fontWeight: 400,
src:
'url("./fonts/my-font.woff") format("woff");'
},
]
};
addBase(fonts);
})],
};
Your plugin looks correct, but the fontFamily defined in the #font-face rule should be the same as the fontFamily defined in the font-family CSS rule. Try changing myfont to My Font in your theme extension config.
See this Tailwind Play REPL for an example.

CSS Modules + Ant design in ReactJs does not work

I am trying to use CSS Modules for CSS styling of my ReactJs project, for this I applied ant design documentation (see here: https://pro.ant.design/docs/style), however unfortunately it doesn't work.
The problem is that I want to override the component style of ant Button and it does not get the style.
Below there is a short sample of my code:
CSS class: in MyContainer.less file:
.antButton{
:global {
.ant-btn-primary {
background-color: rgb(215, 226, 233);
border-color: #848586;
font-size: 7pt;
color: red !important;
}
}
}
code:
import React from 'react';
import { Button } from 'antd';
import 'antd/dist/antd.less';
import styles from './MyContainer.less';
const MyContainer= () => {
return (
<Button type="primary" size="small" className={styles.antButton} >Download</Button>
);
};
export default MyContainer;
I'm using Ant design (Version 4.3.0) in react (Version 16.13.1 ) with Webpack (Version 4.42.0).I also installed less-loader (Version 7.3.0) and babel-plugin-import (Version 1.13.3).
I don't know if there is any specific config of the Webpack that I am missing or the problem is somewhere else?
Finally I could handle my problem with antd, when you use css modules you have to add extra config for antd styles to work, I've found my solution in this web site:
https://www.programmersought.com/article/3690902311/ As described there if you add these configs in these order in your Webpack.config in Rule section it will work with Css modules and overriding less styles of antd components.
{
test: /\.less$/,
include: [/src/],
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
modules: {
localIdentName: "[name]__[local]___[hash:base64:5]",
},
sourceMap: true
},
},
{
loader: require.resolve('less-loader'), // compiles Less to CSS
options: { lessOptions:{javascriptEnabled: true }}
},
],
},
{
test: /\.css$/,
exclude: /node_modules|antd\.css/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
// change
modules: {
localIdentName: "[name]__[local]___[hash:base64:5]",
},
},
},
{
loader: require.resolve('postcss-loader'),
options: {
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
flexbox: 'no-2009',
}),
],
},
},
],
},
{
test: /\.css$/,
include: /node_modules|antd\.css/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1,
// change
// modules: true, // new support for css modules
// localIndetName: '[name]__[local]__[hash:base64:5]', //
},
},
{
loader: require.resolve('postcss-loader'),
options: {
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
flexbox: 'no-2009',
}),
],
},
},
],
},
Also you need to add babel import in your package.json:
"babel": {
"presets": [
"#babel/preset-env",
"#babel/preset-react",
"#babel/preset-typescript"
],
"plugins": [
[
"import",
{
"libraryName": "antd",
"libraryDirectory": "lib",
"style": true
}
]
]
},
and you have to set style to the wrapper div of antd Components in this way:
import React from 'react';
import { Button } from 'antd';
//import 'antd/dist/antd.less'; you don't need this line when add babel.
import styles from './MyContainer.less';
const MyContainer= () => {
return (
<div className={styles.antButton}>
<Button type="primary" size="small" >Download</Button>
</div >
);
};
export default MyContainer;
I wish it help

Resources