Stencil - React Integration Does Not Support Global CSS Styling

As per the guidance provided in the Stencil docshere, I have established some global CSS variables within src/global/variables.css. This file is currently the sole CSS resource in this particular directory.

Upon attempting to incorporate my components into a React environment, everything functions as expected when working within the Stencil project or when transferring the www/build/ directory contents to a plain JS/HTML project. However, the problem arises when trying to import and utilize them in React. Although the components visibly render, the CSS styles from the global/ directory fail to apply.

While defining and utilizing CSS variables directly within component-specific CSS files proves effective, the same cannot be said for global CSS files.

I suspect there may be an issue with my build process, but I am uncertain of where I might be going wrong.

I've experimented with updating to the latest version of Stencil and ensuring all other packages are up-to-date.

In addition, I attempted to address the issue by including:

styleUrls: [
    "local-component.css",
    "../../global/variables.css"
]

Regrettably, this approach did not yield positive results.

The only workaround that seems to work is adding a CSS link tag pointing to the UNPKG CDN like so:

<link rel="stylesheet" type="text/css" href="https://unpkg.com/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="621715074f06114f120d0122524c524c5b">[email protected]</a>/dist/poc/poc.css"/>

However, attempts to use a local path such as the following prove unsuccessful:

<link rel="stylesheet" type="text/css" href="../node_modules/uwe-ds-poc/dist/poc/poc.css"/>
.

Is resorting to this method the only viable option, or is there something crucial I'm overlooking?

Here's an excerpt from my stencil.config.ts:

import { Config } from '@stencil/core';

export const config: Config = {
  namespace: 'poc',
  globalStyle: 'src/global/variables.css',
  outputTargets: [
    {
      type: 'dist',
      esmLoaderPath: 'loader'
    },
    {
      type: 'docs-readme'
    },
    {
      type: 'www',
      serviceWorker: null // disable service workers
    }
  ],
  copy: [
    { src: 'global' }
  ]
};

The copy property facilitates the transfer of the global directory contents to dist/collection/. Despite this, the issue remains unresolved.

This is what my package.json looks like:

{
  "name": "uwe-ds-poc",
  "version": "0.0.9",
  "description": "Stencil Component Starter",
  "main": "dist/index.js",
  "module": "dist/index.mjs",
  "es2015": "dist/esm/index.mjs",
  "es2017": "dist/esm/index.mjs",
  "types": "dist/types/index.d.ts",
  "collection": "dist/collection/collection-manifest.json",
  "collection:main": "dist/collection/index.js",
  "unpkg": "dist/poc/poc.js",
  "files": [
    "dist/",
    "loader/"
  ],
  "scripts": {
    "build": "stencil build --docs",
    "start": "stencil build --dev --watch --serve",
    "test": "stencil test --spec --e2e",
    "test.watch": "stencil test --spec --e2e --watchAll",
    "generate": "stencil generate"
  },
  "devDependencies": {
    "@stencil/core": "^1.8.1",
    "@types/jest": "^24.0.23",
    "@types/puppeteer": "1.20.2",
    "jest": "^24.9.0",
    "jest-cli": "24.8.0",
    "puppeteer": "1.20.0"
  },
  "license": "MIT"
}

The complete Stencil project can be accessed here.

Your assistance is greatly appreciated.

Answer №1

Utilizing the unpkg content delivery network is a viable choice.

Alternatively, when employing a bundler such as webpack or parcel, you have the option to import your CSS by using a statement like

import 'my-package/dist/path/to/global.css'
.

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

Place a button in relation to the top of its parent element

I am trying to display control buttons at the top of a table cell when hovering over an image within the cell. Here is the HTML structure I have: <table id="pridat_fotky"> <tbody> <tr> <td class=" ...

The setting of the custom user agent in the Chrome Extension Manifest Version 3 is not functioning correctly

We currently have an extension that consists of only two files: manifest.json and background.js Despite the browser (Chrome version 112) not reporting any errors, we are facing an issue where the user agent is not being set to 'my-custom-user-agent&a ...

Failed to render props after making API request

I'm in the process of setting up a recipe display, but I'm having some trouble getting it to work properly. I'm using a Rails API to fetch the recipes, but currently nothing is showing up on the page. Below is the code for my recipe contain ...

Restrict access to table records by specifying an array of row identifiers

Hi everyone, I've encountered a small issue. Just to provide some background, I have a table with checkboxes. Each row in the table has an associated ID, and when selected, I receive an array like this: const mySelectedRoles = [1997, 1998, 1999] Once ...

Retrieve information from json, divide it, and transfer it to the chart for display

Greetings everyone! In my project, I am parsing a JSON file from an online API. However, I have encountered a roadblock while trying to split the data. Despite searching extensively on platforms like YouTube, I haven't been able to find a solution tha ...

Understanding the functionality of scope in AngularJS is essential for mastering the framework

Why does this code work? app.controller("ctrl", function($scope){ $scope.From = "Santa"; $scope.To = "Claus"; }); And why doesn't this one work? app.controller("ctrl", function(scope){ scope.From = "Santa"; scope.To = "Claus"; }); ...

What is the best way to monitor a document variable that is set after a component has been

Is there a way to access the variable document.googleAnalytics, added by Google Tag Manager, for A/B testing on a Vue.js page? I attempted to initialize the variable in both the mounted and created methods of the component, but it returns as undefined: ex ...

Transform the Curly Braces within a string into an HTML span Element using JSX

Looking to parameterize a string like 'Hello {name}, how are you?' in React Component? Want to replace curly braces with variable text and highlight it using span/strong tag. Here's an example of the desired final result: Hello <span cla ...

Using the Unleash Feature server in a browser for React projects: A step-by-step guide

I'm currently working on implementing the unleash feature toggle in my React Project. Everything is running smoothly with the backend server (thanks to the Java SDK), but I've hit a roadblock when it comes to making unleash requests from the brow ...

Is there a way to activate all steps simultaneously in material-ui?

I'm currently utilizing material-ui within my React project and am looking to implement a stepper component. However, I would like all steps to be active simultaneously without the need for previous and next buttons to navigate between them. import R ...

Troubleshooting the issue of "Mismatched transaction number*" in MongoDB and Node.js

While trying to add data, I encountered an issue with modifying two schemas using ACID transactions in MongoDB with Node.js. Upon running the program, an error was displayed: (node:171072) UnhandledPromiseRejectionWarning: MongoError: Given transaction n ...

Returning to the Starting Point using React Hooks

I'm in the process of developing a registration form and sharing a portion of my code: const Signup = () => { const [username, setUsername] = useState('') const [email, setEmail] = useState('') const [password, setP ...

retrieve the date value of Focus+Context by using the Brushing Chart

Currently, I am engaged in analyzing the sentiment of tweets on Twitter. The analysis will produce an overall area graph that allows me to choose a specific date range and extract all or some of the tweets falling within that range. In order to create a t ...

React.js Form Validation issue: When using input type "number", the text field remains a single value even after attempting to delete characters with the backspace key

Experiencing difficulty in clearing the input field value, even after pressing backspace the value remains constant. One particular value persists in the input field. import * as React from "react"; import { Button, Form } from "react-boots ...

Is it possible to add animated text using anime.js?

var elements = document.querySelectorAll('.content'); anime({ targets: elements, content: 100, }); <script src="https://raw.githubusercontent.com/juliangarnier/anime/master/lib/anime.min.js"></script> <span class="content"> ...

XMLHttpRequest response: the connection has been terminated

After developing a Flickr search engine, I encountered an issue where the call to the public feed or FlickrApi varies depending on the selection made in a drop-down box. The JSONP function calls provide examples of the returned data: a) jsonFlickrApi({"ph ...

Leveraging TypeScript within the state slices of Redux Toolkit

Exploring the realm of React, I am embarking on a project using NextJs + reduxjs/toolkit along with MUI and TypeScript. I have encountered a hurdle specifically related to TypeScript within reduxjs/toolkit. Here is the section from my file, **store/user-pr ...

Ensure the validity of a field in redux Form by checking it against another field

I am currently utilizing the redux-form library within my project, and I have a Component that contains multiple FieldArray elements. One of these FieldArray components generates a table structure similar to what is shown in the screenshot provided. In eac ...

Error encountered with Next.js and Square API: The Fetch API cannot load due to the unsupported URL scheme "webpack-internal"

I encountered an issue while attempting to retrieve stock data from the Square API. injectGlobalHook.js:1648 Fetch API cannot load webpack-internal:///./node_modules/@next/react-dev-overlay/lib/internal/ReactDevOverlay.js. URL scheme "webpack-internal ...

Optimal method for conducting Jasmine tests on JavaScript user interfaces

After exploring the jasmine framework for the first time, I found it to be quite promising. However, I struggled to find a simple way to interact with the DOM. I wanted to be able to simulate user interactions such as filling out an input field, clicking ...