Google Polymer dynamic removing class - polymer

I already have this template
<app-drawer-layout>
<!-- Drawer content -->
<app-drawer transition-duration="500" swipe-open>
<div style="width: 100%; height: 100%; position: relative;">
<div style="height: 100%; width: 100%; overflow: auto;">
<div id="head-of-drawer-space"></div>
<div id="controls">
<paper-button noink on-click="_pageLoader1">
<div class="menu-item">Load Element 1</div>
</paper-button>
<paper-button noink on-click="_pageLoader2">
<div class="menu-item active">Load Element 2</div>
</paper-button>
<paper-button noink on-click="_pageLoader3">
<div class="menu-item">Load Element 3</div>
</paper-button>
</div>
</div>
</div>
</app-drawer>
</app-drawer-layout>
I want to remove an element with 'active' class when one of other button clicked and add 'active' class to the clicked button.
How should I do?

Here is a vanilla Javascript solution that should work with Polymer elements:
Add an event handler on click to every buttons;
In that callback, for each button, add (or remove) the class to the div inside if it corresponds (or not).
Example:
//select all buttons
var buttons = controls.querySelectorAll( 'paper-button' )
//attach event handlers
buttons.forEach( function ( button )
{
button.onclick = toggle
} )
//add or remove class to every buttons
function toggle ()
{
buttons.forEach( function ( button )
{
var cl = button.querySelector( 'div' ).classList
if ( this == button )
cl.add( 'active' )
else
cl.remove( 'active' )
}, this )
}
You can get the same result with arrow functions:
//select all buttons
var buttons = controls.querySelectorAll( 'paper-button' )
//attach event handlers
buttons.forEach( b => b.onclick = function ()
{
//add or remove class to every buttons
buttons.forEach( b => b.querySelector( 'div' ).classList[ this==b ?"add":"remove"]( 'active' ) )
} )
div.active {
color: red ;
}
<div id="controls">
<paper-button noink on-click="_pageLoader1">
<div class="menu-item">Load Element 1</div>
</paper-button>
<paper-button noink on-click="_pageLoader2">
<div class="menu-item active">Load Element 2</div>
</paper-button>
<paper-button noink on-click="_pageLoader3">
<div class="menu-item">Load Element 3</div>
</paper-button>
</div>

Related

How to compare two classes with jquery?

I want to compare two classes if they're equal to display the content. I have three btns with three different classes and three other divs with three similar classes to buttons. I want to check if Over the button is equal to over the div,then i want to show the element inside over the div, and hide the other elements,and when i click on the under the button i want to do the same, I have tried with Jquery to compare to first get the class name from the button and get the class name from the div and compare them to each other and then give the active class to the one that i want to show it but it seems that i have something wrong
var className = $(this).attr('class');
var tabContent = $('.tab-content').hasClass(className);
var classNameBtnsName = $(this).hasClass(className);
$('button').click(function () {
if (tabContent === classNameBtnsName) {
$('.tab-content').addClass("active-content");
} else {
$('.tab-content').removeClass("active-content");
}
})
.active-content h1{
display:block;
}
h1{
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="btns-container">
<button class="over">Over</button>
<button class="under">Under</button>
<button class="other">Other</button>
</div>
<div class="tab-content over active-content">
<h1 >Show Over elements</h1>
</div>
<div class="tab-content under">
<h1>Show Under elements</h1>
</div>
<div class="tab-content other">
<h1>Show Other Elements</h1>
</div>
I wouldn't use the class of the buttons to specify which content to toggle. It is better to use a data attribute for that to prevent future developments that add more classes to bug your functionality. Then you can use this data attribute for toggling by removing the active class from all and then adding the class to the element belonging to the clicked button.
$('button.togglebutton').on('click', (e) => {
$('.tab-content').removeClass('active-content');
$('.tab-content.' + $(e.currentTarget).data('active-content')).addClass('active-content');
});
.active-content h1 {
display: block;
}
h1 {
display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="btns-container">
<button data-active-content="over" class="togglebutton">Over</button>
<button data-active-content="under" class="togglebutton">Under</button>
<button data-active-content="other" class="togglebutton">Other</button>
</div>
<div class="tab-content over active-content">
<h1>Show Over elements</h1>
</div>
<div class="tab-content under">
<h1>Show Under elements</h1>
</div>
<div class="tab-content other">
<h1>Show Other Elements</h1>
</div>
Inside the click handler callback function, remove the active-content class from all .tab-content elements first, and then add it to the one that also has the button's class.
$('.btns-container button').on('click', function() {
$('.tab-content').removeClass('active-content');
$('.tab-content.' + this.className).addClass('active-content');
});
.active-content h1{
display:block;
}
h1{
display:none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="btns-container">
<button class="over">Over</button>
<button class="under">Under</button>
<button class="other">Other</button>
</div>
<div class="tab-content over active-content">
<h1 >Show Over elements</h1>
</div>
<div class="tab-content under">
<h1>Show Under elements</h1>
</div>
<div class="tab-content other">
<h1>Show Other Elements</h1>
</div>
Assuming .tab-content is hidden by default, you can try something like this:
$('button').click(function() {
// Get class from clicked button
var btnClass = $(this).attr('class');
$('.tab-content').each(function() {
// If tab-content has same class as button, show this
if ($(this).hasClass(btnClass)) {
$(this).show();
} else {
$(this).hide();
}
});
});

Creating a list of items, assign them id and than make a option button with overlay with multiple options

I'm working on a list of items for my website.
I have a list of 5 items with a 'id'. When you click the button, an overlay must be shown with 2 buttons 'change to background to red' and than 'cancel'.
If you click the cancel, the specific 'div class="item"' with the specific id his background must become red.
But, the problem is I don't know how using jquery/javascript to know which button of the div what pressed (button of item id 1 or 2 or 3..)
And also when you click outside the buttons, the overlay must be removed.
Here's the code
$(document).ready(() => {
$('.options-btn').click(function ()
{
var id = $(this).parent().attr('id'); /* find <div class="item"> */
$('body').append('<div class="overlay"></div>');
var append = `
<div class="item-options-active">
<button class="feed-option-btn-number background-btn" tabindex="0">Background set to RED</button>
<button class="feed-option-btn-number cancel-btn" tabindex="0">Cancel</button>
</div>
`;
$(append).appendTo('.overlay');
});
$(document).click(function (e)
{
if (document.getElementsByClassName("overlay").length == 1)
{
if(document.getElementsByClassName("item-options-active").length == 1)
{
// this condition is not working when you click the specific button
if($(".background-btn").data('clicked'))
{
// how to get <div> of the button which was press to change the background??
$('.item').css('background', 'red');
}
}
}
});
})
body {
background: grey;
}
.item {
background: green;
border: 5px solid purple;
margin: 20px;
}
.item button {
margin-left: 10px;
}
.overlay {
position: fixed;
width: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0,0,0,.85);
z-index: 10000;
}
<body>
<div class="show-items">
<div class="item" id="1">
<button type="button" class="options-btn">check options</button>
</div>
<div class="item" id="2">
<button type="button" class="options-btn">check options</button>
</div>
<div class="item" id="3">
<button type="button" class="options-btn">check options</button>
</div>
<div class="item" id="4">
<button type="button" class="options-btn">check options</button>
</div>
<div class="item" id="5">
<button type="button" class="options-btn">check options</button>
</div>
</div>
</body>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
If you rewrite the .option-btn click event to something like this and delete the document click event it should work:
$('.options-btn').click(function ()
{
var id = $(this).parent().attr('id'); /* find <div class="item"> */
var button = $(this);
$('body').append('<div class="overlay"></div>');
var append = `
<div class="item-options-active">
<button class="feed-option-btn-number background-btn" tabindex="0">Background set to RED</button>
<button class="feed-option-btn-number cancel-btn" tabindex="0">Cancel</button>
</div>
`;
$(append).appendTo('.overlay');
$('.overlay').on('click', function(){
$(this).remove();
}).find('.cancel-btn').on('click', function(){
$(this).closest('.overlay').remove();
});
$('.overlay').find('.background-btn').on('click', function(){
button.closest('.item').css('background', 'red');
$(this).closest('.overlay').remove();
});
});
Working fiddle: https://jsfiddle.net/qntupzj6/

How to define onClick event in react properly?

I tried to set an onClick event to open a flexbox in react using tsx. The button and the flexbox is shown properly but vscode shows a problem with the onClick event. I cant figure out whats wrong but maybe you can. I am new to the field and tried some ideas from the stack community but it didnt work for me.
The console tells me I have to assign 'string' but it doesnt work either.
//Function to change visibility of the flexBox
document.getElementById("OpenProfiles")
.addEventListener("click", ProfilesOpn);
function ProfilesOpn () {
var a = document.querySelectorAll(".ProfilesOpen")[0];
var b = document.querySelectorAll(".ProfilesClose")[0];
a.style.visibility = "hidden"
b.style.visibility = "visible";
}
//the button code inside the flexbox
<div className={"Profiles"}>
<div className={"Profile1"}>
<div className={"Profile1P"}></div>
<h3 className={"ProfileH3"}>Profile1</h3>
</div>
<div className={"Profile2"}>
<div className={"Profile2P"}></div>
<h3 className={"ProfileH3"}>Profile2</h3>
</div>
<div className={"Profile3"}>
<div className={"Profile3P"}></div>
<h3 className={"ProfileH3"}>Profile3</h3>
</div>
<div className={"Profile4"}>
<div className={"Profile4P"}></div>
<h3 className={"ProfileH3"}>Profile4</h3>
</div>
<h3 className={"EndCoPro"}>Are you missing any profiles?</h3>
<button id="OpenProfiles" onClick="return(ProfilesOpn());">
<div className={"ProfilesOpen"}><img src={ProfilesOpen} alt="Open Profiles"/></div>
</button>
</div>
//the code in sass for the styling
.Profiles {
position: absolute;
display: flex;
left: 0px;
bottom: 0px;
width: 300px;
height: 900px;
flex-direction: column;
justify-content: flex-start;
align-items: space-between;
background-color: #292929;
box-shadow: 0px 0px 5px 0px rgba(0, 0, 0, 0.2);
border-top-right-radius: 25px;
border-bottom-right-radius: 25px;
visibility: hidden;
}
Code Sandbox: https://codesandbox.io/s/dazzling-bouman-62hgi?file=/src/App.js:0-1850
Here are 2 approaches to what you are trying to accomplish using react hooks:
The ProfilesOpn function uses a ref to set DOM properties.
The reactWayToShowCat function sets the showCat status using internal component state.
import React, { useState, useRef } from "react";
import "./styles.css";
export default function Explorer() {
const a = useRef(null);
const b = useRef(null);
const [showCat, toggleShowCat] = useState(true);
const ProfilesOpn = () => {
a.current.style.visibility = "hidden";
b.current.style.visibility = "visible";
};
const reactWayToShowCat = () => {
toggleShowCat(!showCat);
};
return (
<div className="Profiles">
{Array(4)
.fill("")
.map((_, i) => {
const num = i + 1;
return (
<div className={`Profile${num}`} key={num}>
<div className={`Profile${num}P`}></div>
<h3 className="ProfileH3">{`Profile${num}`}</h3>
</div>
);
})}
<h3 className="EndCoPro">Are you missing any profiles?</h3>
<button id="OpenProfiles" onClick={ProfilesOpn}>
<div className={"ProfilesOpen"} ref={a}>
<img src="https://placekitten.com/200/300" alt="Open Profiles" />
<p>
Click to see solution that uses refs to accomplish what you were
doing
</p>
</div>
</button>
<button id="CloseProfiles" onClick={reactWayToShowCat}>
<div className={"ProfilesClose"} ref={b}>
<>
{showCat && (
<img src="https://placekitten.com/200/300" alt="Close Profiles" />
)}
<p>
Click to see one react way to show and hide the cat (no styling)
</p>
</>
</div>
</button>
</div>
);
}
The main issue in original code was that onClick needs to be set with this syntax:
<button id="OpenProfiles" onClick={ProfilesOpn}>
Hope this helps!

Moving buttons to the bottom of an element

I am using Material UI to create cards that take an argument Actions which is a list of buttons.
The length of the Card is relative to the text I enter, but all Cards will be the same height.
I am very new to CSS and still wrapping my mind around position: fixed, relative, absolute.
This is the code that renders the Card:
export function ViewCurrentPitches2(props) {
const actions = [
<FlatButton
label="Cancel"
primary={true}
onClick={props.closeEditPitch}
/>,
<FlatButton
label="Save"
primary={true}
keyboardFocused={true}
onClick={props.savePitchBeingEdited}
/>,
];
return (
props.state.savedPitches.map((pitch, i) => {
return(
<Card key={pitch.id} className = 'form-margin card-width' zDepth={3}>
<CardText>{pitch.subject} </CardText>
<CardText className='card'>{pitch.pitch}</CardText>
<CardActions>
<FlatButton label="Edit" onClick={(e) => {props.toggleEdit(e, pitch); console.log(props.state.pitchBeingEdited)}}/>
<Dialog
className="dialogBox"
title="Test"
modal={false}
actions={actions}
open={props.state.editPitch}
contentStyle={customContentStyle}
autoScrollBodyContent={true}
>
<TextFieldExampleCustomize currentValue = {props.state.pitchBeingEdited} updateNewPitch = {props.updatePitchBeingEdited} />
</Dialog>
<FlatButton label="Delete" onClick={(e) => {props.deletePitch(e, pitch)}} />
</CardActions>
</Card>
)
})
)
}
<div className='card-parent'>
<ViewCurrentPitches2
state= {this.state}
deletePitch = {this.deletePitch}
handleSave={this.dialogBoxSave}
toggleEdit = {this.toggleEdit}
closeEditPitch = {this.closeEditPitch}
updatePitchBeingEdited = {this.updatePitchBeingEdited}
savePitchBeingEdited = {this.savePitchBeingEdited}
/>
</div>
This is what it looks like:
Can anyone explain to me
1.) When I'm adding in the CSS position: relative | fixed | absolute ...etc what is happening? I assign that to the child correct?
2.) If I want to move the buttons to the bottom of the Card, Card is the parent and I put the styling on the button? How would I go about doing this?
Generally speaking, you would assign relative to the parent and absolute to the child. The child is being positioned absolutely, relative to the parent.
Refer to full documentation for more details.
.card{
display:inline-block;
width:200px;
height: 100px;
border: 1px solid red;
position: relative;
}
.buttons{
position: absolute;
bottom: 0;
border: 1px solid blue;
width: 100%;
}
<div class="parent">
<div class="card">
<div class="buttons">
<button>Edit</button>
<button>Delete</button>
</div>
</div>
<div class="card">
<div class="buttons">
<button>Edit</button>
<button>Delete</button>
</div>
</div>
<div class="card">
<div class="buttons">
<button>Edit</button>
<button>Delete</button>
</div>
</div>
<div>

Polymer 1.0 a/custom element under toolbar clicking bug

I am encountering some problems with a custom element created by me, called <little-game></little-game>.
This is <little-game></little-game> template code :
<template>
<a href="{{link}}">
<paper-material elevation="1">
<paper-ripple></paper-ripple>
<iron-image src="{{img_url}}"></iron-image>
<div id="description">{{name}}</div>
<div id="category">{{category}}</div>
</paper-material>
</a></template>
And the :host css of this element:
:host {
display: inline-block;
text-decoration: none;
z-index:1;
}
Those <little-game></little-game> elements are displayed in a page and inside this page i have a <paper-scroll-header-panel> and a <paper-toolbar>. The problem is when i scroll down and the .tall <paper-toolbar> gets smaller, i can click through the <paper-toolbar> on <little-game>/<paper-ripple> element.
<paper-ripple> css :
paper-ripple {
z-index:1;}
mainToolbar html :
<paper-toolbar id="mainToolbar" class="tall">
<paper-icon-button id="paperToggle" icon="menu" paper-drawer-toggle></paper-icon-button>
<span class="flex"></span>
<!-- Toolbar icons -->
<!--paper-icon-button icon="refresh"></paper-icon-button-->
<paper-icon-button icon="more-vert"></paper-icon-button>
<!-- Application name -->
<div class="middle middle-container center horizontal layout">
<div class="app-name">App title</div>
</div>
<!-- Application sub title -->
<div class="bottom bottom-container center horizontal layout">
<div class="bottom-title paper-font-subhead">App subtitle</div>
</div>
</paper-toolbar>
mainToolbar css :
#mainToolbar {
z-index:3;}
So the main problem is about that i can click the <little-game></little-game> element through the toolbar.
There is an image to understand what i am talking about in a better way:
I think you need to cancel the tap event from propagating through, try adding an on-tap event handler on the paper-toolbar e.g.
<paper-toolbar id="mainToolbar" class="tall" on-tap="{{cancelEvent}}">
then add the function to cancel it
cancelEvent: function(event) {
event.stopPropagation();
}