Alpine JS dynamic style attribute on rollover - css

I have 2 colour pickers (one for the background and one for the text colour).
They are linked to the styles of a button
<button :style="`background-color: ${bg_colour}; color: ${text_colour};`">TEST BUTTON</button>
This works fine.
I would like to swap these variables when the button is hovered over or is active. i.e. the background colour becomes the text colour and the text colour becomes the background colour.
Any suggestions? Is this possible?
Thanks in advance!

We can use the #mouseover and #mouseleave events to swap the colors of the button, furthermore we use the object-binding syntax as well. In the x-init directive, we create two $watch listener objects that will update the styles object when the user picks a new color.
<script defer src="https://unpkg.com/alpinejs#3.x.x/dist/cdn.min.js"></script>
<div x-data="{bg_color: '#e66465', text_color: '#000',
styles: {'background-color': '#e66465', 'color': '#000'}}"
x-init="$watch('bg_color', val => styles['background-color'] = val),
$watch('text_color', val => styles['color'] = val)">
<div>
<input type="color" x-model="bg_color" id="text">
<label for="text">Background color</label>
</div>
<div>
<input type="color" x-model="text_color" id="bg">
<label for="bg">Text color</label>
</div>
<br>
<div>
<button #mouseover="styles['color'] = bg_color, styles['background-color'] = text_color"
#mouseleave="styles['color'] = text_color, styles['background-color'] = bg_color"
:style="styles">
Hover to swap colours
</button>
</div>
</div>

Related

Purple lines / Conditionnal rendering

I have those purple lines ( as my screen bellow ).
I doing some conditionnal rendering, I'd like to know if I can remove all those purple lines.
I've heard removing all divs and adding React.Fragments might help, this what I've done so far. But the issue is still there.
If anyone could tell where the issue might come from, I'd be grateful !
the main view / Render 1
<div className="user-infos">
<h4><u> Parameters </u></h4>
{ (componentState == "edit") ?
<form >
<Render 3/>
</form>
: (componentState == "index") ?
<Render 2/>
: (componentState == "delete") ?
<React.Fragment>
<p> Are you sure you want to leave us ? </p>
<p> If yes, you have to type your email and press "confirm".</p>
<form>
<input type="text" />
<input type="submit" value="confirm email"/>
</form>
<span>
<button> Delete ur account</button>
<button> Cancel </button>
</span>
</React.Fragment>
: ""
}
</div>
Render 2
return (
<React.Fragment>
<p> Username : John </p>
<p> Timezone : London </p>
<span>
<button> Edit Profile </button>
<button> Delete account </<button>>
</span>
</React.Fragment>
Render 3
return (
<React.Fragment>
<label for="email"> Username :
<input type="text"/>
</label>
<label for="text"> Timezone :
<span>
<select
onChange={options}
<options>
</select>
</span>
</label>
<button type="submit"> Save Profile </button>
<button> Cancel </button>
</React.Fragment>
);
By default, chrome adds styling to your html. In the case of <p>, it adds the following css to your element.
If you want the margin to disappear, you need to set margin: 0; on your <p> element to overwrite chrome's styling.
The purple dash line occurs when there is available space for the elements to expand into. See here. From what I can tell from comparing flexboxes with other display types, the purple dash with purple background is specifically for space inside flexboxes (display:flex in CSS).
Would need to see your CSS to be sure, but I imagine there's justify-content: space-between telling the flexbox to evenly distribute the remaining empty space between the elements as well as flex-direction: column telling the flexbox to stack the child elements vertically.
If that's the case, you can remove justify-content: space-between and use flexbox styling to change things to how you want it to look. I found this website helpful: A Complete Guide to Flexbox

How to use ngStyle with :nth-child?

I am new to Angular, and I am trying to style items after the 6th child but it doesn't work. I can color everything, but when I add the nth-child part it doesn't work. Do I have a wrong syntax?
[ngStyle] = "div:nth-child(n+6){'background-color': 'red'}"
<p *ngIf = "status"> Secret Password is Tuna</p>
<div *ngFor="let clickLog of clickLogs"
[ngStyle]="div:nth-child(n+6){{'background-color': 'red'}}">
{{clickLog}}
</div>
<button (click)="clickEvent()">Show Details</button>
<button [disabled]="!checkIfEmty()" (click)="resetUserName()">Reser Username</button>
</div>
You can use pseudo class in the component CSS file.
app.component.css
div:nth-child(n+6) {
background-color: red
}
But you should have proper structure for it in template.
Or you can use index of ngFor.
app.component.ts
<div *ngFor="let clickLog of clickLogs; index as i"
[ngStyle]="{'background-color': i >= 6 ? 'red' : '' }">
{{clickLog}}
</div>

How to place a label before an input box using css?

I have this html:
<div class="entry-content">
<div class="job_listings" data-location="" data-
keywords="" data-show_filters="true" data-
show_pagination="false" data-per_page="10" data-
orderby="featured" data-order="DESC" data-categories=""
>
<form class="job_filters">
<div class="search_jobs">
<div class="search_keywords">
<label for="search_keywords">Keywords</label>
<input type="text" name="search_keywords"
id="search_keywords" placeholder="Keywords" value=""
/>
</div>
<div class="search_location">
<label for="search_location">Location</label>
<input type="text" name="search_location"
id="search_location" placeholder="Location" value="" />
</div>
I want to place the label Where? before location and What? before keywords using css.
Tried:
label[What?]:before {
content: "search_location";
color: green;
}
Didn't work.
At the moment the label location listed in my html shows up as a placeholder, not a label- likewise for the label search keywords This is fine but i would like those placeholders replacing with, for location London, Berlin, Bristol... and for search keywords Chef, Cleaner, Manager...
It's perhaps clearer if you view at: https://adsler.co.uk/jobs/
Couldn't you just place the label with html? Like this
<div class="entry-content">
<div class="job_listings" data-location="" data-
keywords="" data-show_filters="true" data-
show_pagination="false" data-per_page="10" data-
orderby="featured" data-order="DESC" data-categories=""
>
<form class="job_filters">
<div class="search_jobs">
<div class="search_keywords">
<label style="color: green;">What?</label>
<label for="search_keywords">Keywords</label>
<input type="text" name="search_keywords"
id="search_keywords" placeholder="Keywords" value=""
/>
</div>
<div class="search_location">
<label style="color: green;">Where?</label>
<label for="search_location">Location</label>
<input type="text" name="search_location"
id="search_location" placeholder="Location" value="" />
</div>
Based on the HTML snippet you've provided, your CSS selector label[What?]:before is not going to resolve to anything. Square brackets [] are used to select elements based on one of their attributes (see attribute selector definition). You appear to be trying to pass in a desired value (which doesn't exist yet) as an attribute selector, which is impossible.
Looking at the site, the other trouble you're having is that the labels themselves have been hidden. This is currently in your CSS, so will need to be changed or otherwise overridden:
.job_filters .search_jobs div label {
display: none;
}
Then, as already suggested by Mr Lister, something like this will get you on the right track. I've tested in the browser on your site and it works once the labels have been unhidden:
label[for="search_location"]:before {
content: "Where?";
}
label[for="search_keywords"]:before {
content: "What?";
}
I'm going to assume that your actual intention is for the labels to display but you want to change their existing values from "Keywords" and "Location" using only CSS? It's not achievable. You could use a bit of JavaScript to change the text content, but not by CSS with your current implementation.

How to change div background with color picker angular js

I want to change the background color of a couple of divs based on the choosen color from a colorpicker.
I have tried it using this:
<div ng-style="[id^='color']{background-color: '{{rgbPicker.color}} !important}'">
<div bind-html-compile="body">
<div id="color1"></div>
<div id="color2"></div>
<div id="color3"></div>
<div id="color4"></div>
</div>
</div>
I have the right color code in rgbPicker.color (hex code), but the color is not changing. How can I make this work?
Do you really need / want to go through the ngStyle directive, when all you want is to set a background color? Here is a dead simple example using the default <input> color picker which updates the <body> background in a $watch :
<input type="color" ng-model="color">
$scope.color = '#ffffff'
var body = document.querySelector('body');
$scope.$watch('color', function(newVal) {
body.style.backgroundColor = newVal
})
http://plnkr.co/edit/jdWdaqejBLb1hjXF680f?p=preview
You can replace <body> with any querySelectable' element.

How to override Angular2 tree component CSS

I am working on an angular2 project and in that I am using a third party component, Angular2-tree-component. In that component JS file, they are using color coding of highlighting a particular node with X color code.
I want to modify it with Y color code but unable to do so.
In orginal JS file below is the line of code:
.tree-node-focused > .node-wrapper > .node-content-wrapper { background: #e7f4f9 }
I am trying to override this color coding to some other code for below html code:
<p-panel header="Schema">
<div class="row">
<div class="col-sm-12">
<Tree #tree [nodes]="LHSNodes" [focused]="true" [options]="optionsForLHS" (onInitialized)="getLHSTree(tree.treeModel)">
<template #treeNodeTemplate let-node >
<span>
<input type="checkbox" name="node.data.selected" value="{{node.data.selected}}" (click)= "onCheckBoxClick($event, node, 'LHS')" [(ngModel)]="node.data.selected">
<label *ngIf="!node.data.isEdit">{{node.data.documentUri}}</label>
<input *ngIf="node.data.isEdit" id="node.data.documentUri" value="{{node.data.documentUri}}" [(ngModel)]="node.data.documentUri" type="text" size="10" pInputText (keyup.enter)="updateName(node)" />
</span>
</template>
</Tree>
</div>
</div>
</p-panel>
I am not understanding what should I write in my css file because same line of code is not working with updated value.
Let me know if it needs more clarification.

Resources