Preparing to load a CSS file in a Polymer element based on certain conditions

I have numerous CSS files with various animations, and I am interested in dynamically loading a CSS file in a Polymer element based on the animation attribute:

Is it feasible to bind the {{animation}} value like this:

<polymer-element name="yo-dialog" attributes="animation">
    <template>
        <link rel="stylesheet" href="yo-dialog.css">
        <link rel="stylesheet" href="dialog.css">
        <link rel="stylesheet" href="dialog-{{animation}}.css">

        <div id="somedialog" class="dialog">
            <div class="dialog__overlay"></div>
            <div class="dialog__content">            
                <content> </content>
                <div>
                    <button class="action" data-dialog-close>{{animation}}</button>
                </div>
            </div>
        </div>
    </template>

Sample of how to use the element:

<yo-dialog id="the_dialog" animation="ken">
    <h1>hello world</h1>
</yo-dialog>

Answer №1

When faced with a similar issue, I discovered a quick solution by dynamically adding the link element using code. I ensured that the attribute was set before triggering this action, typically during the domReady event:

Instead of using a direct link tag in your code, you can opt for the Polymer declaration approach:

<polymer-element name="yo-dialog" attributes="animation">
    <template>
        <link rel="stylesheet" href="yo-dialog.css">
        <link rel="stylesheet" href="dialog.css">

        <div id="somedialog" class="dialog">
            <div class="dialog__overlay"></div>
            <div class="dialog__content">            
                <content> </content>
                <div>
                    <button class="action" data-dialog-close>{{animation}}</button>
                </div>
            </div>
        </div>
    </template>
    <script>
      Polymer("yo-dialog", {
        domReady: function() {
          var fileref=document.createElement("link")
          fileref.setAttribute("rel", "stylesheet")
          fileref.setAttribute("type", "text/css")
          fileref.setAttribute("href", "dialog-"+this.animation+".css")
          console.log(fileref)
          this.appendChild(fileref)
          console.log("domReady!")

        }
      });
    </script>
</polymer-element>   

I've included a functional sample on Plunker for reference: http://plnkr.co/edit/IrN2FpDDN4PX1MVQqC1C?p=preview

Answer №2

Absolutely, it can be done. Just make sure to utilize the _ prefix before the href attribute to ensure that it properly resolves any template bindings.

Give this a shot:

<link rel="stylesheet" _href="dialog-{{animation}}.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

Tips for concealing overflow x on a responsive website

Could use some help here. Whenever I switch to responsive mode, the overflow x feature gets enabled and I'm at a loss on how to disable it. I've attempted using @media to disable overflow but unfortunately it's not working as expected. ...

Opera's extra space feature allows users to comfortably browse the

I'm attempting to insert a progress bar inside a td element within my table. Below is the code: <td style="width: 150px;"> <div style="height: 16px; max-height: 16px; overflow: hidden; border: 1px solid #80C622;"> < ...

I must click the web icon in order to open the link with buttons

I am having trouble with the buttons I added for my social media links. When I click on the button, it doesn't direct me to the link unless I click on the icon within the button. <link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7. ...

Guide to importing external CSS styles into Angular 2 using the require method

I'm facing an issue with loading an external css file in different environment files. My setup includes two environments: development and production. Loading the css file locally works fine in development mode, but not in production mode. environment ...

Step-by-step guide to displaying the dropdown menu button content at the top of the page in main.html by using the frameset tag

The layout of my main.html involves displaying menu.htm and welcome.htm using frameset. One issue that arises is with the drop-down menu buttons "Admin..." and "Scheduler...". When hovering over these buttons, the dropdown content does not display properly ...

Ways to conceal the contents of a page behind a transparent background div while scrolling

I created a Plunkr with the following markup: <body> <div class="container-fluid"> <div class="row"> <div class="col-sm-12"> <nav class="navbar navbar-fixed-top"> <div class="container-fluid"& ...

changing the elements' classes by using a carousel

Having trouble with a custom carousel and unable to use the standard Bootstrap carousel. This is how my code is structured: Images: <img src="1.img"/> <img src="2.img"/> <img src="3.img"/> Prev / Next buttons: <div class="left ...

Is there a way to adjust the height of a DIV based on the background image and keep the text centered at the same time?

Currently in the process of designing a landing page for my website, and I am interested in incorporating a Jumbotron to provide a more modern and tech-savvy touch. Successfully managed to center my text both horizontally and vertically, now looking into s ...

Can a submit button be created that integrates both a radio button and submission functionality?

Is there a way to combine a radio button and submit button into one element for submitting the value just with a click? Currently, I have a radio button and a separate submit button in my code, but I want to streamline it. This is the current code snippet ...

Tips for concealing the header menu when scrolling on a Drupal website

Hey there, I need some help with hiding the header div while scrolling in Drupal. <header class="header<?php print theme_get_setting('header_top_menu') && !$one_page ? '' : ' header-two'; ?>"> <div clas ...

What steps can I take to guarantee that my grid always accommodates its child without being too small?

My current setup involves utilizing a grid layout to arrange elements in the z-direction. However, I have encountered an issue where a child element extends beyond the boundaries of the grid element: #z-stack { display: grid; border: 5px solid ...

Positioning elements vertically and float them to the left side

I'm struggling to grasp the concept of the float attribute. Here's the code that is causing me confusion: #p1 { border: solid black 3px } <p id="p1" style="float:left">Paragraph 1</p> <a href="https://facebook.com" style="floa ...

Footer button overrides list components due to improper implementation of vertical ion-scroll

Having some trouble setting up ion-scroll on a specific screen in my mobile application built with Ionic. On the Book page of my app, I'm encountering two main issues: https://i.stack.imgur.com/MnheG.png 1) The placement of the Confirm button doesn& ...

Create a Boostrap navbar with a form and links aligned to the right using the navbar

I'm trying to use navbar-right on a navbar-form and a navbar-nav, but the form ends up on one row and the nav links on another row on the right. How can I make the navbar display the brand on the left and have a search field followed by nav links on t ...

Creating a Responsive Bootstrap Webpage

I attempted to replicate a sign-in page using Bootstrap from this link: Prior to this, I had already created a page without using Bootstrap code. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Mainpage.aspx.cs" Inherits="project.Mainpage" ...

Utilizing AJAX to dynamically update a div's content by extracting a specific div from the retrieved data

Although I believe my code is correct, I am not very familiar with AJAX and have been struggling for hours to get it right. I've tried various approaches, including using filters, but nothing seems to work. The issue I'm facing is that the chat m ...

Interacting with CSS buttons using Selenium

Currently, I am working on a test case in Java where I am using Selenium to record a series of events and then play them back on an application. Here is the code snippet I have: // *The app opens a new window* // Get handle of the main window Stri ...

Element obscured by dropdown content now clearly visible thanks to dropdown adjustment

Something strange is happening here - the Add question div shouldn't be visible. It's somehow appearing behind the dropdown content. I've attempted to adjust the z-index of the Add Question div, setting it to a lower number than the dropdown ...

Delete the label portion from the paper text area

Is it feasible to eliminate the excess space occupied by the label in a polymer text-area? Your assistance is greatly appreciated. ...

Javascript on-page scroll positioning

My current dilemma involves finding a solution to optimize in-page scrolling for mobile users. I have a sticky header on the page, which causes #section1 to place the scroll position behind the sticky header. Is there a way to adjust the scroll position b ...