Leveraging vanilla web components in combination with a bootstrap template

Currently, my ASP.NET MVC website is styled using the Metronic Bootstrap template.

I am contemplating the idea of revamping this website by implementing the vanilla web components approach.

In a preliminary test, I created a web component without shadow DOM and noticed that it seamlessly adopted the Metronic style from its parent page.

However, upon enabling shadow DOM, the CSS inheritance ceased, causing my web component to no longer reflect the Metronic style.

Now faced with this dilemma, I am unsure about the best approach: Should I forego shadow DOM to maintain the design consistency at the expense of full encapsulation benefits provided by web components?

How do you suggest handling the integration of a comprehensive Bootstrap template like Metronic with web components? Is the effort worthwhile, or are there specific guidelines for navigating such a scenario?

Answer №1

When working with large CSS frameworks in ShadowDOM based Web Components, it's important to include the necessary CSS within each component to ensure its styling is maintained.

ShadowDOM was specifically created to isolate internal styles from external CSS, so any specific styling required for the ShadowDOM must be included within the component itself.

The easiest way to achieve this is by using the <link> tag within the ShadowDOM, just as you would outside of the components.

class MyElement extends HTMLElement {
  constructor() {
    super();
    let shadow = this.attachShadow({mode: 'open'});
    shadow.innerHTML = `
    <link href="my.css" rel="stylesheet">
    <h2 class="my-header">My Component</h2>
    <button class="my-button">Pay me</button>`;
  }
}

customElements.define('my-element', MyElement);
<link href="my.css" rel="stylesheet">
<div class="my-thing">Stuff</div>
<button class="my-button">Click me</button>
<hr/>
<my-element></my-element>

Without the presence of a file named my.css, there won't be any visible changes rendered.

This approach ensures that the CSS is applied both on the main page and within the Components themselves.

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

Issue encountered: React module not detected while attempting to execute npm start command

While developing my react app, I encountered an issue when trying to run 'npm start'. The application was working as expected until I faced a bug that prompted me to update my node version for a potential fix. After upgrading to node v16.13.2 and ...

Problems with Styling the Header Navigation

Currently facing two CSS challenges on techprosecurity.com that have been difficult to resolve. The header NAV menu displays a slight space when hovering over links that are not active, and I've searched for a solution without success. For instance: ...

Creating numerous presentations without the need for excessive copying and pasting

I recently created a slideshow by modifying some code I found from this original source. For those interested, here is the link to view the slideshow on jsfiddle: View full source code here The issue I encountered is that when attempting to display two ...

Creating a Dropdown Menu: Connecting Multiple Buttons to a JavaScript Function

I am currently working on creating a dropdown menu. I have already written the HTML and Javascript for two buttons, but I'm not sure how to connect the second button to the Javascript function. Do I need to write another function()? <div class="dr ...

Is it necessary for material-ui useStyles to use the entire props object?

Perusing through the documentation, it suggests that in order for our component to accommodate style overrides using classes, we are advised to provide the entire props object to the useStyles hook: function Nested(props) { const classes = useStyles(prop ...

Inconsistent positioning of CSS tooltips when using insertBefore() function

I've been working on integrating tooltips for each event within a FullCalendar resource timeline. However, I'm facing an issue where the tooltips are getting obscured by neighboring events. To provide more context, two screenshots illustrating th ...

Immutability in ASP.NET 5 (also known as Core 1.0) Configuration

I am encountering difficulties in getting ASP.NET 5 (Core 1.0) to work with immutable configuration classes. Let me start by showcasing the process using a mutable class. config.json { "Name": "Lou", "Age": 30 } Config.cs (mutable) public clas ...

Encountering the error "undefined object" while using the yield keyword in JavaScript

var pi = document.getElementById("pi"); function * calculatePi(){ let q = 1; let r = 0; let t = 1; let k = 1; let n = 3; let l = 3; while (true){ if (4*q+r-t < n*t){ alert(n); yield n; ...

Track the amount of views on an AngularJS page and display the total count

Looking to add a view count feature to my website using angularjs. Similar to stackoverflow's question view counter, I want it to be persisted. I'm considering intercepting the http request, updating the backend asynchronously, and then updatin ...

What causes the device pixel ratio to vary across different web pages on the same device and operating system?

I am curious about why device pixel ratio varies between different websites and operating systems. For instance, when using the same device, this site displays a different pixel ratio on Windows compared to Ubuntu. On Windows, the pixel ratio is 1.34 whi ...

Opting to monitor element visibility over relying solely on page load with Neustar WPM

I need help with writing a Neustar WPM script to time the button click until an overlay appears. Here is what my current script looks like: var webDriver = test.openBrowser(); var selenium = webDriver.getSelenium(); webDriver.get('https://www. ...

Handling click events for parent and child elements in Angular 5

In Angular5, Below is the code I am working with: <div (click)="abc()"> some content <button (click)="xyz()">Click</button> </div> When I click on the button, both methods are being called. I want only a sin ...

Tips for properly formatting large numbers

I am facing a challenge with handling large numbers in my JavaScript code. I came across the nFormatter function for formatting large numbers but I am unsure how to integrate it into my existing code. Below is the nFormatter function that I found: functi ...

Test the functionality of clicking on buttons in a specific order within a given set

I need assistance with a unique task involving a series of buttons in my Intern functional test. Each button needs to be tested sequentially, but I'm not sure how to go about it. In my code snippet below, you can see the structure of my page: <inp ...

Expanding an abstract class with Typescript

In order to create a new instance of a base abstract class that implements an interface, I have the interface, the base class, and some properties in an Angular component. Surprisingly, no errors are thrown in the browser (perhaps due to JavaScript's ...

What is the reason for the enhanced sharpness of fonts in Chrome compared to other browsers?

I couldn't find an answer to this question through my searches on Google, so I apologize if it's already been asked. I've been working on a web project recently and one thing I noticed is that the fonts appear much bolder in Firefox or Safar ...

Learn how to grant permissions to staff members by utilizing checkbox options in asp.net C#

In the process of developing a web application using asp.net c#, I am faced with the task of assigning tasks to employees within the system. This project involves three different types of employees, each with their own set of privileges: basic, supervisor, ...

Show a specific form field based on the chosen option in a dropdown menu using Angular and TypeScript

I am looking to dynamically display a form field based on the selected value from a dropdown list. For instance, if 'first' is chosen in the dropdown list, I want the form to remain unchanged. However, if 'two' is selected in the drop ...

npm is having trouble locating the module called 'encoding'

I've run into a npm issue on my newly formatted Ubuntu OS laptop while using VSCode. The error I encountered during installation is: npm ERR! code MODULE_NOT_FOUND npm ERR! cannot find module 'encoding' // Log 0 info it worked if it e ...

How to create line breaks in Angular Material matTooltip elements?

I am currently utilizing Angular 7 along with Angular material matTooltip. My goal is to have the tooltip display each element on a separate line, similar to this: https://i.sstatic.net/faVoo.png However, I am encountering an issue where it displays thr ...