Utilize alternate SCSS file depending on the direction of the text property

I have a dilemma with two SCSS files in my project - one named app.scss and the other app-rtl.scss.

The main distinction between them is that the latter includes SCSS files for handling right-to-left layouts. My goal is to switch to the app-rtl.scss file when the dir attribute is set to rtl.

In terms of technology, I am using Node.js primarily for the backend and React for the frontend.

If anyone has any suggestions on how I can approach this issue, I would greatly appreciate it.

Update:

To elaborate further, I initially used the create-react-app package and then ejected to incorporate SCSS. This is how I currently import the stylesheet (as a single file).

index.js

import '../scss/app.scss'; // importing all styles here

Below is a snippet from the app.scss file:

// Setting default value for $dir as ltr
$dir: ltr;

// Importing helpers and libraries
@import "base/directional";
@import "base/variables";
@import "bootstrap/functions"; 
@import "bootstrap/mixins"; 
...

The app-rtl.scss contains various SCSS files specific for right-to-left design.

If I opt to use Webpack, how can I compile these into two separate CSS files and implement the recommended solution provided? Alternatively, if dealing with this through Node.js is more feasible, I am open to exploring that route as well.

P.S: While my knowledge of Webpack is limited, I believe the following configuration could serve the purpose:


{
    test: /\.scss$/,
    loaders: [
        require.resolve('style-loader'),
        require.resolve('css-loader'),
        require.resolve('fast-sass-loader')
    ]
},
{
    test: /\.css$/,
    loaders: [
        require.resolve('style-loader'),
        require.resolve('css-loader'),
    ]
}

Answer №1

To handle different text directions, I would organize my stylesheets into three: one for normal direction, a shared styles file to be imported into both, and then the right-to-left (rtl) stylesheet.

// shared.scss
@import "bootstrap/functions";
@import "bootstrap/variables";
@import "bootstrap/mixins";
@import "bootstrap/root";
@import "bootstrap/reboot";
@import "bootstrap/type";
@import "bootstrap/images";
@import "bootstrap/code";
@import "bootstrap/grid";
@import "bootstrap/tables";
@import "bootstrap/forms";
@import "bootstrap/buttons";
@import "bootstrap/transitions";
@import "bootstrap/dropdown";
@import "bootstrap/button-group";
@import "bootstrap/input-group";
@import "bootstrap/custom-forms";
@import "bootstrap/nav";
@import "bootstrap/navbar";
@import "bootstrap/card";
@import "base/variables";

//default.scss
@import "shared.scss"

//rtl.scss
@import "shared.scss"
@import "base/directional";

Then, you can import them into your React app like this:

if (RTL) {
  require('rtl.scss')
} else {
  require('default.scss')
}

For further information, please visit: React RTL. Conditional Import CSS

Answer №2

I found a solution by creating two separate CSS files using the npm package extract-text-webpack-plugin.


      {
        test: /style\/.*\.scss$/,
        use: styleCss.extract({
          use: ['css-loader', 'sass-loader']
        })
      },
      {
        test: /app-rtl\/.*\.scss$/,
        use: styleRtlCss.extract({
          use: ['css-loader', 'sass-loader']
        })
      },
      {
        test: /commonStyle\/.*\.scss$/,
        use: ['css-loader', 'sass-loader']
      },

Based on the value of the directory, I loaded one of the styles.

Routers.js

class Routes extends Component {
constructor(props) {
    super(props);

    this.state = {
        stylePath: loadStyle(props.direction)
    }
    const defaultLanguage = props.customer.defaultLang || LOCAL_LANGUAGES[0].code;

    props.InitializeDefaultLang(defaultLanguage);
    props.getVehicles();
    props.getCountriesOnly(defaultLanguage);
    props.changeDefaultDirection(defaultLanguage);
}
componentDidUpdate = (prevProps, prevState) => {
    if (prevProps.direction !== this.props.direction) {
      this.setState({
        stylePath: loadStyle(this.props.direction)
      })
    }
  }

return (
     <div>
       <link rel="stylesheet" type="text/css" href={this.state.stylePath} />
     </div>
 )

config.js

export default function loadStyle(direction) {

  if (direction === 'ltr') {
    return '/style.css';

  } else {
    return '/style-rtl.css';
  }
}

This solution may be helpful for others facing the same issue. However, there is a slight delay in switching between styles, so I am considering using Node instead.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

View, Show, and Add to a Different Table

I recently started learning NodeJS programming and I've developed a script that reads from a Database table named nodetest containing 50,000 records. The script then displays the data in the browser and writes it to another table called 'nodetest ...

Guidelines on navigating a blank page using the Nuxt-js method

I have run into an issue in my Nuxt.js project where I need to open a link in a new target when a user clicks a button. Despite trying various solutions, I haven't been able to find a workaround within the Nuxt.js framework itself. <a :href=&qu ...

Fetching data from multiple URLs with an unknown quantity asynchronously

What would be the most efficient approach for performing an unknown quantity of asynchronous GET requests? Imagine you have a base URL, '', and you want to execute a series of asynchronous requests to various paths like: [ 'http://www. ...

Should the secret for express-session be a fixed value or should it change dynamically?

I'm uncertain whether the secret for this application should remain static or if it can be dynamic. Currently, I am setting the secret as follows: var salt1 = bcrypt.genSaltSync(); var salt2 = bcrypt.genSaltSync(); var secret = bcrypt.hashSync(salt1 ...

Rotation of table columns slider

My goal is to design a table layout with 4 columns, but only display 3 columns at a time. I plan to achieve this by using a div that spans the width of the 3 visible columns and applying overflow: hidden to hide the last column. When the button is clicked, ...

Using scope in ng-style to manipulate a portion of a CSS property value in Angular

I've been attempting to add a border using ng-style, but I'm struggling to figure out how to concatenate the value from the scope variable. None of the methods below seem to be working for me: <div ng-style="{'border-top' :'1p ...

I have a simple inquiry regarding the customization of table designs

I struggle with html/css, so I could really use some assistance. Here's a fiddle of a table link to fiddle. The td rows are currently the same width, but I want to set specific widths for each column - 200px for the left column and 50px for the right ...

Can a border not be added to a <tr> in an HTML table using CSS for any specific reason?

I am facing an issue with the borders of a table row (tr) when applying CSS styling. The tr has a class called "underRow" which is supposed to add a border at the bottom. In my CSS, I have defined the following for the ".underRow" class: .underRow { ...

showing the upload preview and disabling automatic uploading in Dropzone with React

Currently, when the user clicks the upload button and selects a file, it automatically uploads the file. However, I do not want this automatic upload to happen. Instead, I want to display the selected files to the user first before uploading them. I need ...

Is there a barrier I've reached with the amount of hashtags I'm using?

My goal is to limit the use of javascript and rely more on CSS to achieve desired effects on my website. One feature I have developed is a modal that opens using hashtags and targets, similar to this example: http://codepen.io/maccadb7/pen/nbHEg. While th ...

Tips for converting PDF files to images or reducing PDF file size using JavaScript

In my web application built with Next.js, I have a requirement where users can upload PDF files. However, I need to compress these files to a smaller size (e.g. 1MB). Despite searching extensively, I haven't found a satisfactory solution that meets my ...

Tips for making a CSS sprite clickable using the <a> tag instead of relying on Javascript

Can you create a click-able CSS sprite without the use of Javascript? Here's an example: Check out the live demo here. HTML: <div></div> CSS: div { width: 100px; height: 100px; background-image: url(http://perfectwebtutori ...

Displaying the Ionic loading spinner on the entire page rather than a restricted small box

Right now, I'm able to implement a basic loading effect, but it appears as a small box. presentCustomLoading() { let loading = this.loadingCtrl.create({ content: 'Loading Please Wait...' }); loading.present(); } However, I ...

JSON database leaderboard feature for leaders

I’ve implemented a leveling system with the code shown below. It utilizes a JSON database to organize data in this format: {"626916199783071750":{"xp":94,"level":12} I am looking to create a command that displays a leaderboa ...

What could be the reason for the compatibility issues between CSS/SASS modules and a third-party library?

I am puzzled by the fact that my sass modules do not affect the third-party component I am using, specifically react-horizontal-scrolling-menu <ScrollMenu selected={selected} scrollToSelected={true} data={items && items.map((item: any) ...

RectAreaLight in Three js does not produce any light reflection when used with MeshPhongMaterial due to lack of support for OES_texture_half

After trying to incorporate a RectAreaLight into my three.js scene where I have objects with MeshPhongMaterial, I noticed that there is no light reflection on the objects. A useful example can be found here: Link If you open the developer tools, you can s ...

Unable to interact with button within a clickable div

Currently, I am encountering an issue with Chakra UI in React where the functionality of a Box component is being executed instead of a Button when clicked inside it. Despite trying to adjust the z-index, the problem persists. Here's my code snippet: ...

What is the best way to utilize partials when developing an express template engine?

As I develop my custom template engine for expressjs in node.js, I am facing a challenge related to rendering partials within a view. According to the documentation (http://expressjs.com/guide.html#view-partials), I want to render these partials while stil ...

"Surprising quirks in the functionality of Bootstrap's image gallery

My goal is to create a matrix layout of 30 group member photos using HTML, CSS, and Bootstrap. Unfortunately, some of the photos are not aligning properly and are appearing in the wrong block, leaving some blocks empty. All the photos have the same aspect ...

Adjust webpack configuration in CRA 3.x without the need to eject

Looking to tailor the webpack config in CRA 3.x without having to eject. My goal is to create a custom build output in an external directory. One solution I came across was using the react-app-rewired plugin, but it appears to only work for CRA versions ...