The Tailwind style did not completely translate when applied to the content of dangerouslySetInnerHtml

I have an HTML file containing Tailwind styles stored in my database, which needs to be fetched first. This will result in an HTML string that will be inserted into dangerouslySetInnerHtml. Here is the code snippet (utilizing Qwik):

export const useHTML = routeLoader$(async (path) => {
    // const domainName = path.url.host
    let domainName = path.url.host
    let htmlFile = ''

    if (path.url.host === 'localhost:5173') {
        domainName = 'page.id'
    }

    if (path.pathname) {
        htmlFile = `linkToMyFile/index.html`
    } else {
        htmlFile = `linkToMyFile/index.html`
    }

    const responseHtml = await fetch(htmlFile);

    return (await responseHtml.text() as string)
});

export default component$(() => {

    const htmlContent = useHTML().value;

    const htmlString: string = htmlContent.toString();

    return (
        <>
            <div dangerouslySetInnerHTML={htmlContent} />
        </>
    )
})

I have also created a custom configuration for styling by modifying the tailwind.config.js to match the style of the fetched HTML content. Here is how the Tailwind config looks:

export default {
  content: ['./src/**/*.{js,ts,jsx,tsx,mdx}'],
  theme: {
    screens: {
        xs: '480px',
        sm: '640px',
        md: '768px',
        lg: '1024px',
        xl: '1280px',
        '2xl': '1400px',
    },
    colors: {
        current: 'currentColor',
        transparent: 'transparent',

        black: '#000',
        white: '#fff',

        //the rest of the code
    },
    fontWeight: {
        hairline: '100',
        thin: '200',
        light: '300',
        normal: '400',
        medium: '500',
        semibold: '600',
        bold: '700',
        extrabold: '800',
        black: '900',
    },
    fontFamily: {
        body: '"Clash Display"',
        heading: '"Clash Display"',
        sans: 'ui-sans-serif',
        serif: 'ui-serif, Georgia',
    },

//the rest of the default style code

The final output did not turn out as expected. It seems like the issue lies within my fetched HTML code placed in dangerouslySetInnerHtml. The Tailwind config is being applied before the fetching process is completed. I attempted to directly input the code in the prop, and it worked fine. However, since the styling needs to be dynamic, the fetching process is necessary.

Answer №1

If your HTML files reside in the same system as where Tailwind compiles, it's recommended to include content file globs that cover these files. Alternatively, if they are in a different location, you might need to explore configuring safelist to instruct Tailwind to generate CSS rules for classes that are not encountered during compilation.

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

Tips for storing and recalling a 24-hour countdown timer using Local Storage

I'm new to JavaScript and I have a 24-hour countdown timer that resets on page reload. However, I want to use LocalStorage to save the starting progress so that it continues running even if the page is closed or refreshed. The goal is for the timer to ...

Leveraging JSON Data for Dynamic Web Content Display

I have been attempting to parse and display the JSON data that is returned from a REST API without any success. When tested locally, the API's URL structure is as follows: http://localhost/apiurl/get-data.php It returns data in the following format ...

Invalid component exception: The element type is not valid in React Native

In my React Native App.js file, I have included the following code snippet import { StatusBar } from 'expo-status-bar'; import React, { useRef } from 'react'; import { StyleSheet, Text, View, Canvas } from 'react-native'; impo ...

Error: The specified module is missing an export that was requested

Hello, I encountered an error in my console that reads: "Uncaught SyntaxError: The requested module './components/tileHeader/controller.js' does not provide an export named 'tileHeader'". Below is the code snippet causing the issue: co ...

The malfunction of Bootstrap modal in iterative scenarios

I am currently developing a comprehensive method for handling AJAX requests in my JavaScript code, but I am facing some issues with the Bootstrap modal not functioning as intended. Here is the HTML structure: <div class="modal fade" id="consult_modal_ ...

Storybook does not seem to be recognizing the @apply feature in Tailwind CSS

I've been utilizing Next CSS modules (sass) and have tried various solutions to no avail. The issue I'm facing is that when running Storybook, the CSS fails to compile the @apply method from Tailwind. While a simple fix would be to remove @apply ...

What is the process for retrieving document field values for an item in Umbraco 7 backoffice?

I have developed a unique Umbraco 7 dashboard where I aim to retrieve specific field details from instances of a particular document type in my Umbraco CMS. After successfully obtaining a list of all documents of the specified type using entityResource.ge ...

Aligning Description Item components horizontally in antdLearn how to easily horizontally align Description

Currently, I am utilizing the `antd` Description components. In this scenario, when there is no `title` for the items, the value should be aligned to the left. You can see an example of this alignment in the image below: https://i.sstatic.net/Ah70f.png I ...

Guide on developing a dynamic Navbar component on Laravel Mix with Vue Js

As I delve into learning and practicing Vue.js, my goal is to create a component that includes elements like Navbar and Footer within a single view. Initially, I set up an index for the first view but faced an issue with adding the navbar as it did not sho ...

Direction of the grid in Tailwind CSS

Here is the grid, displayed like this: 1,2,3 4,5,6 7,8,9 I am looking to adjust the grid within the parent Div to display like this: 1,4,7 2,5,8 3,6,9 (please note that there could be more elements than just 9) <div className="grid h-full grid-co ...

What is the best way to augment an AJAX array response with additional data?

I'm working on an AJAX request and I need to add additional properties to the response I receive. Can anyone offer some guidance? Here is the AJAX request code I'm using: var form_data = {}; $.ajax({ type: "GET", url: "../../../sample_ ...

The integration of socket.io with static file routing in node.js is encountering issues

I am currently developing a chat application and I have encountered an issue. When the static file routing is functioning correctly, the socket.io for the chat feature throws a "not found" error in the console. http://localhost/socket.io/?EIO=3&tran ...

Link property can be added to a bindpopup polygon in Leaflet to have it open in a new tab when clicked

Is it possible to include a hyperlink in popup content on Leaflet, similar to this example? function onEachFeature(feature, layer) { idLoDat = feature.properties.IDLo; layer.bindPopup("Company name: " + feature.properties.TenCty + " ...

Add the file retrieved from Firestore to an array using JavaScript

Trying to push an array retrieved from firestore, but encountering issues where the array appears undefined. Here is the code snippet in question: const temp = []; const reference = firestore.collection("users").doc(user?.uid); firestore .collec ...

Display modal within a React list

I need to display a list of items with an edit button for each item, which should trigger a modal showing the details of that specific item. Initially, I had a single modal component in the parent element and passing the visible values to the parent state ...

Managing simultaneous tasks with multiple event handlers for a single event

Within the realm of HTML, you may encounter an <input type="file"> element that can be linked to one or more event handlers. However, if one event handler includes asynchronous code, it will pause at that point and move on to the next event ...

Storing query for later utilization using form and javascript

I have implemented a function to create charts using the following code snippet: $('#button_submit').click(function() { var start_date = $('#start_date').val(); var end_date = $('#end_date').val(); var type = $( ...

When an absolute positioned DIV is nested inside a relatively positioned DIV, it disappears when the page is scrolled

A unique feature of my DIV is that it remains fixed at the top of the page as you scroll. This is achieved by setting its position to fixed. However, within this main container, I have another div with a relative position. While the content in the relative ...

What's the issue with the submit button not functioning on the form?

After downloading a sample form, I encountered the following code: <form id="login-user" method="post" accept-charset="utf-8" action="/home.html" class="simform"> <div class="sminputs"> <div class="input full"> <l ...

Retrieve information from a form in AngularJS without relying on two-way data binding

Utilizing two-way data binding, the code operates effectively. However, there is a stipulation - instead of using =, use <. Upon the initial launch of the application, the text inputs will contain predefined values. The objective is to enable users to ...