How can I import global SCSS files into Storybook for an Angular application?

Struggling to apply custom global styles in Storybook using theme-default.scss file. Components are loading but styles are not being applied despite following Storybook's official documentation on custom webpack configuration (not very familiar with webpack).

Below is the content of my .storybook/main.js file:

const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  stories: ['../src/**/*.stories.ts'],
  addons: ['@storybook/addon-actions', '@storybook/addon-links', '@storybook/addon-notes'],
  webpackFinal: async (config, { configType }) => {

    config.module.rules.push({
      test: /\.scss$/,
      use: ['style-loader', 'css-loader', 'sass-loader'],
      include: path.resolve(__dirname, '../')
    });

    return config;
  }
};

Encountering errors like mentioned below:

Error message here.

Attempted using inline loader syntax as suggested here. Content of my .storybook/preview.js:

import '!style-loader!css-loader!sass-loader!./_common.scss';

Inside _common.scss:

@import '../src/theme-default.scss';
@import '../src/styles.scss';

No error shown, but styles are still not applying.

Included angular.json file for reference.

{ Angular JSON content here }

Also tried implementing this approach using sass-resources-loader.

Tried various solutions with no success. Any assistance would be highly appreciated. Thank you!

Answer №1

Starting from Angular version 13 and beyond, it is now required to include style imports directly in your storybook-architekt within the angular.json file. These imports should be added under your-project > (build-)storybook > options > styles. The previous method of importing stylesheets in .storybook/main.js is no longer supported.

Example section in angular.json:

"your-project": {
  ...
  "storybook": {
      "builder": "@storybook/angular:start-storybook",
      "options": {
          ...
          "projectBuildConfig": "your-lib:build-storybook",
          "styles": [
              "path-to-the-stylesheet/styles.scss",
              "path-to-the-stylesheet/your-theme.scss"
          ],
          ...
      },
      ...
  
    },
    ...
}

This update is documented in the storybook migration guide.

Answer №2

When using storybook versions earlier than 6.4.*, you can import theme-default.scss into your Angular app's global styles.scss file to easily incorporate it.

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

Events for individual items in Angular using ngFor

TypeScript: displayText: boolean = false; showOnHover(){ this.displayText = true } hideOnLeave(){ this.displayText = false; } Html <ul> <li class="txt-block" *ngFor='let element of elementsToDisplay;let i = index&apo ...

Can you switch out the double quotation marks for single quotation marks?

I've been struggling to replace every double quote in a string with a single quote. Here's what I have tried: const str = '1998: merger by absorption of Scac-Delmas-Vieljeux by Bolloré Technologies to become \"Bolloré.'; console ...

Angular 14: Enhance Your User Experience with Dynamic Angular Material Table Row Management

My inquiry: I have encountered an issue with the Angular material table. After installing and setting up my first table, I created a function to delete the last row. However, the table is not refreshing as expected. It only updates when I make a site chang ...

Issue encountered in Cypress while attempting to locate an identifier beginning with a numeric value

My struggle is with finding an element by id using Cypress. The Cypress selector playground provided the following code: get("#\33 -2") Unfortunately, when I execute this code in Cypress, it results in an error: Syntax error, unrecognized expressio ...

Trouble encountered while attempting to install ng2-bootstrap within my Angular 2 project

I've been attempting to integrate ng-bootstrap into my Angular 2 application for dropdown functionality. However, I'm encountering the following error in the console: Console error Here is the snippet of my System.config.js code: System.config. ...

Refresh the StripeCardComponent upon successful addition of a new card

Using StripeCardComponent in conjunction with ngx-stripe, I'm encountering the issue of resetting the form after adding the Stripe card. Do you have any insights on how to achieve this? <ngx-stripe-card [options]="cardOptions" [elementsOptions]="e ...

Double loading of functions on tab pages

In the work page of my application, I have created a function that is part of one of the tab's pages. This function is designed to be loaded when the page requests data from an API. It should be noted that apart from the tab's pages, the push op ...

The 'formGroup' property cannot be bound as it is not recognized as a valid property of 'form' in Angular 7

I tried implementing a login feature using web API with Angular 7, but encountered an error when using <form [formGroup]="loginForm" (submit)="loginFormSubmit()">. What could be causing this issue? https://i.sstatic.net/3M2a5.jpg login.component.ht ...

Using ngFor in Angular 2-5 without the need for a div container wrapping

Using ngFor in a div to display an object containing HTML from an array collection. However, I want the object with the HTML node (HTMLElement) to be displayed without being wrapped in a div as specified by the ngFor Directive. Below is my HTML code snipp ...

Changes made to the native element do not automatically update the bound variable in ngModel

There exists an Angular Directive that is designed to restrict input into a text field to numbers only. Here's the code for this directive: import { Directive, ElementRef, HostListener, ChangeDetectorRef, Renderer2 } from "@angular/core"; ...

Using Rust WebAssembly in Next.js

Seeking assistance with integrating a rust-generated wasm module into my NextJS project. This is how I am attempting to import the wasm: import init, { tokenize } from "@/wasm/lazyjson"; const Test = dynamic({ loader: async () => { ...

How to prevent value overwriting when adding dynamic fields in Angular 7?

In my current project using Angular, I am tasked with setting up configuration using 4 specific fields: Department Name (select option) Service Name (select option) Status (text input) Level (text input). I need to be able to add multiple sets of these ...

display angular filter for mat radio option selected from mat-radio group

On the screenshot provided below, there are 2 buttons within a mat radio group. The condition is that if modelForm.get('schedule').value equals '1', then only display the radio button with the value 'A'. Otherwise, show all va ...

How can we include the style.scss file within the index.module.scss file in a

I am encountering an issue with my CSS files. I have a file called style.scss: .bg{ background-color: green; } and another file called index.module.scss: .color{ background-color: red; color: white; } @import "style.scss"; In additi ...

"Enhance Your Search with PrimeNG's AutoComplete

Currently, I am attempting to implement the autocomplete feature from primeNg based on their documentation, but I am facing issues with displaying the suggestions. Firstly, I added the AutoComplete module by importing it: import { AutoCompleteModule } fro ...

Enhancing Google analytics integration with Continuous Integration (CI) in Angular 9

Currently, I am working on an app that is being developed on circleCI. Since updating Angular from version 7 to 9, there has been a prompt to install the CLI on circleCI. Google’s Privacy Policy at https://policies.google.com/privacy? For more details ...

Creating dynamic forms in Ionic using form builder and binding control names

Currently, I am working on creating a radio button group with a unique form group name and control. My situation involves changing a select option, triggering an AJAX call that dynamically loads values or creates a new set of radio button groups. Everyth ...

watchWebpack is compiling all the files

As per the webpack documentation on watching files webpack can keep an eye on files and recompile them whenever there are changes. My understanding is that this implies webpack will only compile the files that have been modified. I have a configuratio ...

Unable to locate module within the ngModule imports

I am facing a peculiar issue. I have used npm install to add ng-push, imported PushNotificationsModule from 'ng-push', and included PushNotificationsModule in my ngModule imports. Interestingly, both the ng build and ng build --prod commands are ...

What could be causing this TypeScript code to not pass typechecking?

defining two objects in TypeScript with different property sets and assigning them to each other. Link to more information ...