Sharing libraries among different web components can be achieved by following these steps

Embarking on a micro frontends project using custom elements has sparked the need to find a way to share dependencies across all parts of the application.

I am particularly interested in integrating the Material-ui library into this structure. One idea is to attach the library to the window object and access it within the custom element (web component).

However, I'm faced with a challenge as material-ui exists as an npm module and I have yet to discover a method to load it using a script tag.

The custom elements are being loaded via http import, meaning they will be loaded before the window object is populated with the material-ui library.

Answer №1

One effective method of sharing libraries, particularly UI libraries like Material-UI, is to place them on the global scope (using the window object).

In this way, a web component (or custom element) included in the composition layer can access these libraries from the inherited window object within its context.

This approach works well for functionalities-only scenarios. However, when dealing with UI libraries that include styles, one must consider whether the web component has shadow dom enabled and turn it off if necessary.

The reason behind this is the shadow dom's ability to scope styles within the web component, allowing for easy inheritance from parent elements.

If you're interested, check out a sample I created last year which I recently updated and got working again :) github.com/mnemanja/web-components-composition

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

Looking for a Webpack setup for a React Components Library that includes Typescript, SASS, CSS Modules, and SASS support

I'm on the lookout for a functional Webpack configuration tailored for a React Components Library that includes TypeScript, SASS, and CSS Modules with SASS support. If anyone has one they'd like to share, I would be extremely grateful! ...

What is the best way to define a function in React hooks - using a function statement or

When working with a react hook and needing to define a function inside it, which approach is preferable? useEffect(() => { //... function handler() {} //... }, []); or should I use the newer const declaration instead? useEffect(() => { ...

React - Attempting to access property X from an undefined variable

Trying to access the string in section1.text, but my console is showing: https://i.stack.imgur.com/ox8VD.png Here's the JSX code I'm using: return ( <div> <h1>{this.props.article.title}</h1> ...

"Initiate React app with specific port number using the Command Line Interface

I'm trying to launch my react server on Linux CLI with a specific port number without modifying the package.json script. I want the ability to run multiple react instances on different ports via CLI. I've come across suggestions like npm start ...

styling not affecting SVGIcon component in MaterialUI React

I am experiencing an issue with applying a style to the ActionSearch icon. Despite my efforts, the style does not seem to be applied as expected. Although the Paper component receives styles without any problems, the icon component seems to be resistant t ...

The construction of the Gatsby site encountered a major obstacle

I've been facing challenges while trying to build my Gatsby site. Whenever I run 'gatsby develop' in the console, my app starts without any issues. However, when I attempt to build it, I encounter errors like the ones shown below. Does anyon ...

How can I show an item repeatedly with each button click in ReactJS?

Currently, I have a setup where I can display a checkbox and its label upon clicking a button. However, the limitation is that only one checkbox can be displayed at a time. Is there a way to modify this so that I can show multiple checkboxes simultaneous ...

Remove a record from a SQL database using a React front-end application

I'm facing an issue with deleting a row from an SQL table using input from a React front end. I had previously succeeded in this task, but after updating the columns in the table, I am struggling to replicate the delete operation. It seems like my req ...

Exploring the wonders of Next.js and its ability to incorporate

I am currently working on a Next.js (13.3.0) project and facing an issue with global styles that include animations. Here is the structure of my folders: https://i.stack.imgur.com/nM5xw.png All SCSS files are loaded through the master.scss file: @import & ...

Guide on setting up a route in Next.js

Recently, I developed a simple feature that enables users to switch between languages on a webpage by adding the language code directly after the URL - i18n-next. Here's a snippet of how it functions: const [languages, ] = React.useState([{ langua ...

What is the process for managing data synchronization in Redux-persist?

I developed an authorization application that writes data to AsyncStorage after a successful login. I now want to access this data through shared storage and am wondering how I can transfer or remove data synchronization with Redux-persist. ...

Styling the Padding of MUI Select Component in ReactJS

Currently, I am facing an issue with the Material UI Select component where I am trying to decrease the padding within the element. The padding seems to be a property of one of the subclasses .MuiOutlinedInput-input, but my attempts to change the padding h ...

What are the best practices for managing data input effectively?

I am facing a challenge with input validation. I need to restrict the input to only accept strings of numbers ([0-9]) for the entity input field. If anything else is entered, I want to prevent it from overwriting the value and displaying incorrect input. I ...

Need Some Help Reversing the Direction of This CSS Mount Animation?

Exploring this fiddle, I encountered a small div showcasing an opacity animation. The animation smoothly executes when the state transitions to true upon mounting the component, causing the div to fade in beautifully. However, I faced a challenge as the an ...

Error: module 'next/react' not found

Recently delving into next.js, I encountered an error after creating a project with npx create-next-app. Oddly enough, the app still runs despite this issue. { "resource": "/E:/codes/news-website/news-website/pages/index.js", ...

Best practice for incorporating types into a Redux-toolkit reducer

As someone who is relatively new to TypeScript, I have a specific goal in mind. I am looking to create an interface where: interface ActionType { fieldName: {any one key from the interface FormStateType listed below such as 'name', 'age&ap ...

Typescript Next.js Project with Custom Link Button Type Definition

I have a project that includes a custom Button component and a NextLink wrapper. I want to merge these two components for organization purposes, but when I combine the props for each, I encounter an issue with spreading the rest in the prop destructuring s ...

Encountering various instances of "There are multiple modules with names that only differ in casing."

I'm currently working on a project using NextJS and Antdesign for the frontend development. As I attempt to create a basic login feature utilizing the Form components provided by AntDesign, I am encountering a barrage of errors. Specifically, I am see ...

Ways to avoid route change triggered by an asynchronous function

Within my Next.js application, I have a function for uploading files that includes the then and catch functions. export const uploadDocument = async (url: UploadURLs, file: File) => { const formData = new FormData(); formData.append("file" ...

What is the standard practice for configuring a React and Express web application?

Currently, I am embarking on a project that involves creating a Single Page Application (SPA) using React, Node, and Express. Initially, I contemplated the idea of utilizing Node/Express to serve static files, including writing a react app and hosting it v ...