How to style paper-input with certain mixins (polymer 2)? - css

I'm trying to style a paper-input based on a design I got. I used some of the custom proprieties described here , but not all of them work.
I have problems using --paper-input-container-label and --paper-input-container-input-focus.
Maybe I try to use them the wrong way or it requires some extra steps.
Here is my code
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../paper-input/paper-input.html">
<link rel="import" href="../paper-input/paper-input-container.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<dom-module id="first-element">
<template>
<style>
paper-input {
--paper-input-container-color: rgb(64, 64, 64);
--paper-input-container-focus-color: rgb(64, 64, 64);
--paper-input-container: {
border: none;
padding: 0px;
}
--paper-font-subhead: {
font-size: 100%;
}
--paper-input-container-underline-focus: {
display: none;
}
--paper-input-container-underline: {
display: none;
}
--paper-input-container-input: {
height: 24px;
line-height: 20px;
padding: 0 4px;
border: 1px solid rgb(194, 198, 199);
}
--paper-input-container-input-focus: {
border-color:red;
}
--paper-input-container-label {
font-weight: bold;
}
--paper-input-container-invalid-color: red;
}
:host {
display: block;
}
</style>
<paper-input always-float-label label="Floating label"></paper-input>
<paper-input label="username">
<iron-icon icon="icons:accessible" slot="prefix"></iron-icon>
<div slot="suffix">#email.com</div>
</paper-input>
</template>
<script>
class FirstElement extends Polymer.Element {
static get is() { return 'first-element'; }
static get properties() {
return {
prop1: {
type: String,
value: 'first-element'
}
};
}
}
window.customElements.define(FirstElement.is, FirstElement);
</script>
</dom-module>

I am not sure what result you are expecting so it is hard to help you, but one thing I am sure it's your style tag is so messy. And you need to write a semi-colon after mixins because they are like css properties and every css properties are semi-colon separated.
Try your code like that :
<style>
:host {
display: block;
}
paper-input {
--paper-input-container-color: rgb(64, 64, 64);
--paper-input-container-focus-color: rgb(64, 64, 64);
--paper-input-container: {
border: none;
padding: 0px;
};
--paper-font-subhead: {
font-size: 100%;
};
--paper-input-container-underline-focus: {
display: none;
};
--paper-input-container-underline: {
display: none;
};
--paper-input-container-input: {
height: 24px;
line-height: 20px;
padding: 0 4px;
border: 1px solid rgb(194, 198, 199);
};
--paper-input-container-input-focus: {
border-color: red;
};
--paper-input-container-label: {
font-weight: bold;
};
--paper-input-container-invalid-color: red;
}
</style>
Another thing, you are trying to style the border color of the input on focus but also you are setting display to none which doesn't make sense. You have to remove display: none and add border-color: red; to --paper-input-container-underline-focus if you want to style the color.

Related

Pseudo-element and pseudo-class in component

I'm trying to style a native component.
These two work:
:host::-webkit-scrollbar-thumb {}
:host(:hover) {}
but I don't know how to fix the following one:
:host::-webkit-scrollbar-thumb:hover {}
I tried to put the parentheses in different places to no avail.
EDIT: SAMPLE CODE
The sample code below uses the Shadow DOM and the styles are attached to it.
This is only supported in Chrome, which is not a problem for me since I'm writing an Electron app.
Note that
:host::-webkit-scrollbar-thumb:hover {
background: black;
}
doesn't work, while
<style>
#elem::-webkit-scrollbar-thumb:hover {
background: black;
}
</style>
does.
The last one is a workaround as it breaks encapsulation. In a complex app built from components, that's highly undesirable.
Here's the sample code WHICH WORKS ONLY IN CHROME:
JSFiddle
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<style>
#elem::-webkit-scrollbar-thumb:hover {
background: black;
}
</style>
<script>
const css = `
:host {
position: absolute;
overflow: auto;
left: 20px;
top: 20px;
width: 200px;
height: 50px;
}
:host::-webkit-scrollbar {
width: 10px;
background-color: rgb(36, 106, 153);
}
:host::-webkit-scrollbar-track {
background-color: rgb(36, 106, 153);
border-radius: 4px;
}
:host::-webkit-scrollbar-thumb {
background: linear-gradient(rgb(253, 253, 134), rgb(165, 224, 243));
border-radius: 4px;
border: solid 1px rgb(6, 115, 193);
}
:host::-webkit-scrollbar-thumb:hover {
background: black;
}
`;
function f() {
const span = document.createElement('span');
span.append('fdsasdfafsdafsdafdsadfsadfsadfsadfsafdsadsadasdsa');
const root = document.createElement('div');
root.append(span);
root.id = 'elem';
const shadowRoot = root.attachShadow({ mode: 'open' });
shadowRoot.append(...root.childNodes);
const styleSheet = new CSSStyleSheet;
styleSheet.replaceSync(css);
shadowRoot.adoptedStyleSheets = [styleSheet];
const body = document.getElementsByTagName('body')[0];
body.appendChild(root);
}
window.onload = () => {
f();
};
</script>
</head>
<body></body>
</html>
:host selects the host of the shadowDOM. Maybe instead of applying styles on the host through the shadowDOM, try directly applying the css to the element the normal way.
So if your host is called .someElement, then .someElement::-webkit-scrollbar-thumb:hover should work. Or just see if you're able to change all scrollbar-thumbs with *::-webkit-scrollbar-thumb:hover
The best solution I could come up with is to avoid :host altogether by wrapping the content of the component in an additional div so that that can be regarded as a virtual root, and we can apply the styles to that instead of the real root.
Encapsulation is preserved and everything works just fine. We just have a redundant div.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<script>
const css = `
#virtroot {
position: absolute;
overflow: auto;
left: 20px;
top: 20px;
width: 200px;
height: 50px;
}
#virtroot::-webkit-scrollbar {
width: 10px;
background-color: rgb(36, 106, 153);
}
#virtroot::-webkit-scrollbar-track {
background-color: rgb(36, 106, 153);
border-radius: 4px;
}
#virtroot::-webkit-scrollbar-thumb {
background: linear-gradient(rgb(253, 253, 134), rgb(165, 224, 243));
border-radius: 4px;
border: solid 1px rgb(6, 115, 193);
}
#virtroot::-webkit-scrollbar-thumb:hover {
background: black;
}
`;
function f() {
const span = document.createElement('span');
span.append('fdsasdfafsdafsdafdsadfsadfsadfsadfsafdsadsadasdsa');
const root = document.createElement('div');
const div = document.createElement('div');
root.append(div);
div.append(span);
div.id = 'virtroot';
const shadowRoot = root.attachShadow({ mode: 'open' });
shadowRoot.append(...root.childNodes);
const styleSheet = new CSSStyleSheet;
styleSheet.replaceSync(css);
shadowRoot.adoptedStyleSheets = [styleSheet];
const body = document.getElementsByTagName('body')[0];
body.appendChild(root);
}
window.onload = () => {
f();
};
</script>
</head>
<body></body>
</html>

Nuxt Js not compiling all CSS rules

My dev environment is working great, but when I tried building the app, some of the styles are missing. Here is an example
<style lang="scss">
.badge {
display: inline-block;
padding: 5px 15px;
border-radius: 25px;
font-size: 0.75em;
#apply tw-bg-border tw-text-primary tw-font-bold;
&--danger {
color: white;
#apply tw-bg-danger-alt;
}
&--info {
color: white;
#apply tw-bg-info;
}
&--warning {
color: white;
#apply tw-bg-warning;
}
}
.badge--success {
color: white;
#apply tw-bg-success-alt;
}
</style>
I need to move .badge--success outside for it to work, but all other CSS modifiers are not working.

CSS for colouring one row of antd table in react

This is the CSS for the antd style I'm using
style.css
.ant-table-tbody > tr > td, .ant-table-thead > tr > th
{
padding:4px;
}
tr:nth-child(odd){
background: #f1e6ff;
}
tr:nth-child(even){
background: white;
}
thead[class*="ant-table-thead"] th{
background-color:#000 !important;
color: white;
font-weight: bold;
border-color: #000;
text-align: center;
}
.table_btn
{
margin:0 !important;
}
.ant-btn
{
margin:0;
}
.ant-table-tbody > tr:hover > td {
color: #fff;
}
index.less
#import "callout";
#import 'e-commerce';
#import "pricing-tables";
#import "login";
#import "dashboard";
#import "error";
#import "editor";
#import "testimonials";
tr:nth-child(odd){
background:inherit !important;
}
.ant-modal-content {
.ant-modal-close{
color: #fff !important;
}
.ant-modal-header {
background-color: #000000;
.ant-modal-title {
color: #fff !important;
}
}
}
.table-wrapper {
.ant-btn {
padding: 0 10px;
height: 30px;
font-size: 13px;
> .anticon {
+ span {
margin-left: 5px;
}
}
&.ant-btn-success {
color: #3d8918;
border-color: #d9d9d9;
&:hover {
background-color:#3d8918;
color: #fff;
}
}
&.ant-btn-danger {
color: #c70d17;
background-color:#fff;
&:hover{
background-color:#c70d17;
color: #fff;
}
}
}
.actions {
text-align: right;
.ant-input {
border-radius: 2px;
padding:0 10px;
font-size: 13px;
height: 30px;
}
}
.table-layout {
.ant-table-small{
> .ant-table-content{
> .ant-table-body {
margin: 0 !important;
> table {
> .ant-table-tbody{
> tr{
> td{
padding: 2px 8px !important;
font-size: 13px !important;
text-align:center;
min-width: 80px;
.ant-btn {
width:100px;
}
}
}
}
}
}
}
index.js
<Table
className="table-layout"
columns={this.state.columns}
dataSource={filteredData}
rowClassName='data-row'
bordered={true}
size={"small"}
onRowDoubleClick={ (record, index, event) => this.handleEditModal(record) }
onRowClick={(record, index, event) => this.handleRowClick(record)}
loading={this.state.loading}
pagination={{ pageSize: 14 }}
/>
This is how Table is used in the index page. style.css and index.less are the pages for CSS.
Can anybody help me to write one CSS in this page for making one row green color ?
I want to make one row green based on condition.
I need the CSS
I need to call the CSS in the page where code is
I found two ways to do this as of now:
One way is to use rowClassName prop of Table:
.table-row-light {
background-color: #ffffff;
}
.table-row-dark {
background-color: #fbfbfb;
}
<Table
rowClassName={(record, index) => index % 2 === 0 ? 'table-row-light' : 'table-row-dark'}
columns={columns}
dataSource={dataSource}
loading={loading}
/>
Second way is to use plain CSS
.table-striped-rows tr:nth-child(2n) td {
background-color: #fbfbfb;
}
.table-striped-rows thead {
background-color: #f1f1f1;
}
<Table
className="table-striped-rows"
columns={columns}
dataSource={dataSource}
loading={loading}
/>
Note that rowClassName works only for rows, so for CSS on table headers we can only use plain CSS like above.
See if these Jsfiddle links helps you. They show 2 ways to change backgroung-color your rows.
https://jsfiddle.net/2b2376a4/
https://jsfiddle.net/2b2376a4/
data: [
{id:1, name: 'one', color: '#fff'},
{id:2, name: 'two', color: '#eee'},
{id:3, name: 'three', color: '#ddd'}
]
for first link
render(text, record) {
return {
props: {
style: { background: record.color },
},
children: <div>{text}</div>,
};
},
for second link
rowClassName={(record) => record.color.replace('#', '')}
if you want to use styled-components you can wrap it like this
const StyledTable: React.FC<TableProps<T>> = styled(Table)`
.ant-table-header th {
background-color: green;
}
`
export const Table = () => (
<StyledTable
columns={columns}
dataSource={data}
...
/>

How to style :root without !important using proper specificity

Inside a Custom Element because border-color is set on the parent page, I can not make border-color work without resorting to !important
:host([player="O"]) {
color: var(--color2);
border-color: var(--color2) !important;
}
The selector works fine, the color is set,
so it is a Specificity issue
Question: Is it possible without !important ?
Working snipppet:
window.customElements.define('game-toes', class extends HTMLElement {
constructor() {
super();
let shadowRoot = this.attachShadow({
mode: 'open'
});
shadowRoot.innerHTML = 'Toes';
shadowRoot.appendChild(document.querySelector('#Styles').content.cloneNode(true));
}
});
:root {
--boardsize: 40vh;
--color1: green;
--color2: red;
}
game-toes {
width: var(--boardsize);
height: var(--boardsize);
border: 10px solid grey;
background: lightgrey;
display: inline-block;
}
<TEMPLATE id="Styles">
<STYLE>
:host {
display: inline-block;
font-size:2em;
}
:host([player="X"]) {
color: var(--color1);
border-color: var(--color1);
}
:host([player="O"]) {
color: var(--color2);
border-color: var(--color2) !important;
}
</STYLE>
</TEMPLATE>
<game-toes player="X"></game-toes>
<game-toes player="O"></game-toes>
qomponents
You are using CSS variable so you can still rely on them like this:
window.customElements.define('game-toes', class extends HTMLElement {
constructor() {
super();
let shadowRoot = this.attachShadow({
mode: 'open'
});
shadowRoot.innerHTML = 'Toes';
shadowRoot.appendChild(document.querySelector('#Styles').content.cloneNode(true));
}
});
:root {
--boardsize: 40vh;
--color1: green;
--color2: red;
}
game-toes {
width: var(--boardsize);
height: var(--boardsize);
border: 10px solid var(--playercolor,grey);
color:var(--playercolor,#000);
background: lightgrey;
display: inline-block;
}
<TEMPLATE id="Styles">
<STYLE>
:host {
display: inline-block;
font-size:2em;
}
:host([player="X"]) {
--playercolor: var(--color1);
}
:host([player="O"]) {
--playercolor: var(--color2);
}
</STYLE>
</TEMPLATE>
<game-toes player="X"></game-toes>
<game-toes player="O"></game-toes>
<game-toes ></game-toes>
As a complement to #Temani excellent answer: it happened because the element CSS style for <games-toes> will supersede the shadow root :host style.
According to Google's presentation:
Outside styles always win over styles defined in shadow DOM. For example, if the user writes the selector fancy-tabs { width: 500px; }, it will trump the component's rule: :host { width: 650px;}

How to override styles from another css file

I have a css file (main.css) and I'd like to override it using another css file (overrides.css). But I have problem doing it as they are in different files and they get different hashes.
This is my css:
/* main.css */
.mainContainer {
padding: 16px;
margin: 16px;
background-color: palevioletred;
border-radius: 5px;
}
.mainContainer h1{
color: white;
}
/* overrides.css */
.mainContainer h1{
color: blue;
}
From here, I used Object.assign() to combine css files but it didn't help. This is my component:
import React from 'react';
import Main from './main.css';
import Overrides from './overrides.css';
const Style = Object.assign({}, Overrides, Main);
class Sample extends React.Component{
render(){
return (
<div className={Style.mainContainer}>
<h1>Hello</h1>
<p>Hello CSS modules!</p>
</div>
);
}
}
export default Sample;
I expect my h1 to become blue but it won't. This is my compiled css:
/* main.css */
._1pXpG {
padding: 16px;
margin: 16px;
background-color: palevioletred;
border-radius: 5px;
}
._1pXpG h1{
color: white;
}
/* overrides.css */
.Wmy0p h1{
color: blue;
}
I expect .Wmy0p h1 to be ._1pXpG h1 so it can override. But it won't. Note that if you just paste the content of overrides.css at the bottom of the main css it will work but I need my override css file to be in a separate file.
Thanks in advance
To over ride styles from other file can be made by giving one more specificity from parent div. Mostly specificity solves to override other files CSS.
class Sample extends React.Component{
render(){
return (
<div className={Style.mainContainerDetails}>
<div className={Style.mainContainer}>
<h1>Hello</h1>
<p>Hello CSS modules!</p>
</div>
</div>
);
}
}
/* main.css */
.mainContainer {
padding: 16px;
margin: 16px;
background-color: palevioletred;
border-radius: 5px;
}
.mainContainer h1{
color: white;
}
/* overrides.css */
.mainContainerDetails .mainContainer h1{
color: blue;
}
You can just use vanilla JavaScript to replace that specific css link on the webpage.
I also suggest using an event listener to wait until the page is loaded & then make the replacement.
Here is an example:
function overrideCommonCss() {
var webpageCurrentLink = "main.css", webpageNewLink = "overrides.css", webpageFoundLink, webpageCssTags = 0,webpageAllCssLinks = document.getElementsByTagName("LINK");
if (webpageAllCssLinks) {
for (webpageCssTags = 0;webpageCssTags < webpageAllCssLinks.length;webpageCssTags++) {
webpageFoundLink = webpageAllCssLinks[webpageCssTags].href;
if (webpageFoundLink.indexOf(webpageCurrentLink) !== -1) {
webpageAllCssLinks[webpageCssTags].href = webpageAllCssLinks[webpageCssTags].href.replace(webpageCurrentLink, webpageNewLink);
break;
}
}
}
}
if(window.attachEvent) {
window.attachEvent("onload", overrideCommonCss);
}
else {
window.addEventListener("load", overrideCommonCss, false);
}
have you tried using the !important in css? I believe that seems to be the problem. Below is an example, in plain html:
/* main.css */
.mainContainer {
padding: 16px;
margin: 16px;
background-color: palevioletred;
border-radius: 5px;
}
.mainContainer h1{
color: white;
}
/* overrides.css */
.mainContainer h1{
color: blue;
}
<div class="mainContainer">
<h1>Hello World!</h1>
</div>
As you can see, using !important works pretty well for me...

Resources