Hi I'm trying to load conditionlly an external css file in my polymer but is not working
the code:
<polymer-element name="my-polymer-component" attributes="query">
<template>
<template if="{{condition}}">
<link type="text/css" rel="stylesheet" href="my-polymer-component.css"/>
</template>
<template if="{{condition}}">
<link type="text/css" rel="stylesheet" href="my-polymer-component2.css"/>
</template>
<section class="container">
<div class="headerImportText">
<span>
hello world
</span>
</div>
<section>
<template>
<script>
Polymer({
});
</script>
</polymer-element>
I need to changed the UI of the component by the condition.
As far as I know, you cannot do declarative conditional CSS loading in the template,
you must do your conditional CSS loading programmatically.
Here you have a sample: http://plnkr.co/edit/8Oavqpbh8eJTTKLw13bL?p=preview
Relevant part of the code:
<polymer-element name="my-polymer-component" attributes="condition">
<template>
<h1>Condition: {{condition}}</h1>
<section class="container">
<content> </content>
</section>
</template>
<script>
Polymer({
domReady: function() {
var fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet")
fileref.setAttribute("type", "text/css")
fileref.setAttribute("href", this.condition+".css")
console.log(fileref)
this.appendChild(fileref)
console.log("domReady!")
}
});
</script>
</polymer-element>
<h1>Here you have a sample</h1>
<my-polymer-component condition="a">
</my-polymer-component>
Hope it helps!
Related
I am cutting my project into several templates. I have a drawer (<app-drawer> from the app-layout package), and I decided to make a template out of it. I would like to open or toggle the drawer with a button in the main template. I tried to use data binding for that, but the drawer doesn't appear.
Here is the minimal code of the main window:
<link rel="import" href="../bower_components/app-layout/app-header/app-header.html">
<link rel="import" href="../bower_components/app-layout/app-toolbar/app-toolbar.html">
<link rel="import" href="../bower_components/app-layout/app-header-layout/app-header-layout.html">
<link rel="import" href="../bower_components/paper-button/paper-button.html">
<link rel="import" href="test-drawer.html">
<dom-module id="test-window">
<template>
<app-header reveals>
<app-toolbar>
<paper-button on-tap="togglemenu">Test</paper-button>
</app-toolbar>
</app-header>
<test-drawer drawerOpened="{{drawerOpened}}"></test-drawer>
</template>
<script>
Polymer({
is: "test-window",
togglemenu: function() {
alert('test')
this.drawerOpened = !this.drawerOpened;
},
})
</script>
</dom-module>
Here is the minimal code of the drawer:
<link rel="import" href="../bower_components/app-layout/app-drawer/app-drawer.html">
<link rel="import" href="../bower_components/app-layout/app-drawer-layout/app-drawer-layout.html">
<link rel="import" href="../bower_components/paper-menu/paper-menu.html">
<link rel="import" href="../bower_components/paper-item/paper-item.html">
<dom-module id="test-drawer">
<template>
<app-drawer id="drawermenu" opened="{{drawerOpened}}" swipe-open>
<paper-menu selected="{{menuSelected}}" attr-for-selected="name">
<paper-item name="item1">Item1</paper-item>
<paper-item name="item2">Item2</paper-item>
<paper-item name="item3">Item3</paper-item>
</paper-menu>
</app-drawer>
</template>
<script>
Polymer({
is: "test-drawer",
properties: {
drawerOpened: Boolean,
},
})
</script>
</dom-module>
I know the callback runs because the alert box shows up.
What am I doing wrong here?
Your property is named drawerOpened, which is mapped to dash-case (i.e., drawer-opened) for data binding, so change this:
<test-drawer drawerOpened="{{drawerOpened}}"></test-drawer>
to:
<test-drawer drawer-opened="{{drawerOpened}}"></test-drawer>
HTMLImports.whenReady(() => {
Polymer({
is: 'test-drawer',
properties: {
drawerOpened: {
type: Boolean,
notify: true
}
}
});
Polymer({
is: 'test-window',
togglemenu: function() {
this.drawerOpened = !this.drawerOpened;
},
});
});
<head>
<base href="https://polygit.org/polymer+1.7.1/components/">
<script src="webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="polymer/polymer.html">
<link rel="import" href="app-layout/app-layout.html">
<link rel="import" href="paper-menu/paper-menu.html">
<link rel="import" href="paper-item/paper-item.html">
<link rel="import" href="paper-button/paper-button.html">
</head>
<body>
<test-window></test-window>
<dom-module id="test-drawer">
<template>
<app-drawer id="drawermenu" opened="{{drawerOpened}}" swipe-open>
<paper-menu selected="{{menuSelected}}" attr-for-selected="name">
<paper-item name="item1">Item1</paper-item>
<paper-item name="item2">Item2</paper-item>
<paper-item name="item3">Item3</paper-item>
</paper-menu>
</app-drawer>
</template>
</dom-module>
<dom-module id="test-window">
<template>
<style>
app-header {
background: #47cf73;
}
</style>
<app-header reveals>
<app-toolbar>
<paper-button on-tap="togglemenu">Test</paper-button>
</app-toolbar>
</app-header>
<test-drawer drawer-opened="{{drawerOpened}}"></test-drawer>
</template>
</dom-module>
</body>
codepen
I am having trouble with the React tutorial. After some help from stackoverflow I was able to get the server/API running, but now for some strange reason base.css is not loading.
base.css is in the css folder where it should be, and when I'm editing in Dreamweaver it automatically loads base.css just fine, so I'm really confused.
Here is my index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>React Tutorial</title>
<!-- Not present in the tutorial. Just for basic styling. -->
<link rel="stylesheet" href="css/base.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.7/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.6.15/browser.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/marked/0.3.5/marked.min.js"></script>
</head>
<body>
<div id="content"></div>
<script type="text/babel">
var Comment = React.createClass({
render: function() {
return (
<div className="comment">
<h2 className="commentAuthor">
{this.props.author}
</h2>
{this.props.children}
</div>
)
}
});
var CommentList = React.createClass({
render: function() {
return (
<div className="commentList">
<Comment author="Pete Hunt"> This is one comment</Comment>
<Comment author="Jordan Walke"> This is another comment</Comment>
</div>
);
}
});
var CommentForm = React.createClass({
render: function() {
return (
<div className="commentForm">
Hello, world! I am a CommentForm.
</div>
);
}
});
var CommentBox = React.createClass({
render: function() {
return (
<div className="commentBox">
<h1>Comments</h1>
<CommentList />
<CommentForm />
</div>
);
}
});
ReactDOM.render(
<CommentBox />,
document.getElementById('content')
);
</script>
</body>
</html>
The problem was I needed to add a leading slashing to the .css file reference.
became
The css was loaded and applied to the page.
I have a custom element called "datasource.html" which loads a json using iron-ajax. Now, I want to use the JSON data into "app.html" and eventually into "categories.html"
Then in app.html, I want to use this same value of myattr and transfer the same to categories.html.
Now, datasource.html prints the data on screen but categories.html shows nothing on the page.
What I want is that I load data in datasource.html once and then use the data anywhere in the project.
Trying to figure this out for last 2 days. Still got nothing.
Thanks in advance.
Here is my index.html file
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="apple-touch-icon" href="apple-touch-icon.png">
<script src="bower_components/webcomponentsjs/webcomponents.js"></script>
<link rel="import" href="lgelements/datasource.html">
<link rel="import" href="lgelements/app.html">
</head>
<body>
<dom-module id="landing-page" attributes="myattr">
<template>
<lg-datasource myattr="{{myattr}}"></lg-datasource>
<lg-app myattr="{{myattr}}"></lg-app>
</template>
</dom-module>
<script>
Polymer({
is: "landing-page",
});
</script>
<landing-page></landing-page>
</body>
</html>
Here is datasource.html
<link rel="import" href="../bower_components/iron-ajax/iron-ajax.html">
<dom-module id="lg-datasource" attributes="myattr" hidden>
<template>
<iron-ajax url="../data.json" auto handleAs="json" last-response="{{myattr}}"></iron-ajax>
<template is="dom-repeat" items="[[myattr]]">
<a href="[[item.url]]" target="_blank">
<h1>[[item.name]]</h1>
</a>
</template>
</template>
</dom-module>
<script>
Polymer({
is: "lg-datasource",
});
</script>
Here is my app.html
<link rel="import" href="categories.html">
<dom-module id="lg-app" attributes="myattr" vertical layout>
<template is="auto-binding">
<h1>[[myAttr]]</h1>
<lg-categories myattr="{{myattr}}"></lg-categories>
</template>
</dom-module>
<script>
Polymer({
is: "lg-app",
});
</script>
And here is categories.html
<dom-module id="lg-categories" attributes="myattr">
<template is="auto-binding">
<h2>MY NAME IS MANIK</h2>
<template is="dom-repeat" items="{{myattr}}">
<a href="[[item.url]]" target="_blank">
<h1>[[item.name]]</h1>
</a>
</template>
</template>
</dom-module>
<script>
Polymer({
is: "lg-categories",
});
</script>
Use double curly brackets {{}} when you use the item inside the element with is="dom-repeat". So change it to:
<template is="dom-repeat" items="{{myattr}}">
<a href="{{item.url}}" target="_blank">
<h1>{{item.name}}</h1>
</a>
</template>
Also you should expose the myattr as a property and remove the template in datasource.html file.
Polymer({
is: "lg-datasource",
properties : {
myattr : Array
}
});
I have only skimmed your code, but I had the same kind of problem,... a child element that was the datasource and the parent that was supposed to display it. To get the data binding to work I needed to call _setPropertyName(data) in the child. Can't seem to find where this is documented but that's how its done internally in iron-ajax (look at the "loading" property).
I'm playing with Polymer 1.0 by creating a simple custom-element (ui-button).
<link rel="import" href="../../../../bower_components/polymer/polymer.html">
<dom-module id="ui-button">
<link rel="import" type="css" href="ui-button.css">
<template>
<button class="ui button">
<template is="dom-if" if="{{icon}}"><i class="icon">1</i></template>
<template is="dom-if" if="{{label}}">{{label}}</template>
</button>
</template>
</dom-module>
<script src="ui-button.js"></script>
Everything works fine in Chrome, but in Firefox the button has no styling.
My guess is that the problem is the external stylesheet, because when i put the CSS inline (style-tag)...it works.
Is this a bug in Polymer 1.0?
I really want to use the external stylesheet...
This is working for me in both Chrome and Firefox. This is my index.html file:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/stylesheet.css">
<script src="/bower_components/webcomponentsjs/webcomponents-lite.min.js"></script>
<link rel="import" href="/app/general/your-element.html">
</head>
<body>
<your-element></your-element>
</body>
</html>
Inside the element, I'm using the css class / id references just like you would on any regular html tag. So, inside of your-element looks something like this:
<link rel="import" href="/bower_components/polymer/polymer.html">
<dom-module id="your-element">
<template>
<span class="some_class_style">Hey, I'm stylish!</span>
</template>
<script>
Polymer({
is: 'your-element',
...
});
</script>
</dom-module>
And, it's styled by whatever you defined for some_class_style. Keep in mind, I've only made elements that are one level deep (ie. no children elements), but it seems to be working fine.
Lets assume I have two routes which require two different css as follows.
/home
needs
<head>
<link rel="stylesheet" href="/home.css">
</head>
And
/dashboard
needs
<head>
<link rel="stylesheet" href="/dashboard.css">
</head>
I can't put both css in same head file as they are conflicting. I can't do something as below.
<head>
<link rel="stylesheet" href="/home.css">
<link rel="stylesheet" href="/dashboard.css">
</head>
Is there any way to put different css for different route's in head?
Note: I need css in head to avoid glitch.
You can use different layouts for the two routes.
<template name="css1">
<head>
<link rel="stylesheet" href="/dashboard.css">
</head>
<div class="main">
{{> yield}}
</div>
</template>
and
<template name="css2">
<head>
<link rel="stylesheet" href="/home.css">
</head>
<div class="main">
{{> yield}}
</div>
</template>
and use it in the layout option for the route like:
this.route('path to css1', {
path: '/',
layoutTemplate: 'css1',
template: "css1 template"
});
Or alternatively:
You could do:
<template name="layout">
<head>
{> yield region="css"}}
</head>
<div class="main">
{{> yield}}
</div>
</template>
and
this.route('path to css1', {
path: '/',
layoutTemplate: 'layout',
template: "css1 template"
yieldTemplates: {
'temple having css1': {to: 'css'}
}
});
Meteor compiles all your CSS in a way similar to Rails' asset pipeline.
Your question is coming from a non-single page app mindset.
You can put different stylesheets for different pages, in a multiple page site. But how will this work for an app that is only one page?
Instead, you should scope your style rules.
IE, instead of .container { color: blue }, do something like .container .home-container { color: blue }.