Change parent properties if CSS child exists

Asked

Viewed 361 times

2

I have the following CSS:

.react-tagsinput .react-tagsinput-input {
  width: 100% !important;
}

// coloquei aqui apenas para lembrar que existe essa opção caso desejar usar
//.react-tagsinput--focused .react-tagsinput-input {
//  width: 100% !important;
//}

input:focus::-webkit-input-placeholder { color:transparent; }
input:focus:-moz-placeholder { color:transparent; } /* FF 4-18 */
input:focus::-moz-placeholder { color:transparent; } /* FF 19+ */
input:focus:-ms-input-placeholder { color:transparent; } /* IE 10+ */

I have the following problem:

Set . React-tagsinput-input as 100% for the placeholder to appear in full, otherwise it was only appearing with 80px.

But by clicking outside the input, the field still exists below.

What I desire is: When clicking outside the field, if there is . React-tagsinput-tag as child, set . React-tagsinput-input as 0px, ie to disappear the empty line below. When clicking again, be set to 100% again.

There is also . React-tagsinput-focused . React-tagsinput-input if needed for the clicked field.

The first two images are as I wish.

Sem nenhuma tag

Com tag (Tudo certo por aqui) - class mudou para .react-tagsinput--focused

PROBLEMA AQUI - ao clicar fora, gostaria que essa linha sumisse se já existe alguma tag ao perder o foco. Ao ganhar foco novamente, ela aparece para ser digitado

PROBLEM IS HERE IN THIS LAST IMAGE - when clicking outside, I would like this line to disappear if there is already a tag when losing focus. When gaining focus again, it appears to be typed again.

  • There are some ways to do yes only with CSS, so I understood if you have some tag or nothing you want the line to have a height x, if you have a tag or nothing and focus the height is 2x, and Blur back to x having something inside or not, that’s it?

  • It’s not really about height, it’s about whether she shows up or not. What I want is: delete . React-tagsinput-input IF there is any . React-tagsinput-tag when taking Focus. In the image with the red arrow, as it lost focus, the right one was to disappear the . React-tagsinput-input. When you click again, it should appear, as in the second image. If you don’t have any . React-tagsinput-tag, it should also appear, as it contains the placeholder, as in the first image. That is, if it does not exist. React-tagsinput-tag, whether with or without focus, . React-tagsinput-input must appear.

  • I forgot to comment that the blank line actually just doesn’t have the placeholder because in React I changed it if there was already a tag inserted. Therefore, if this modification were not made, it would be as follows: http://prntscr.com/od1mz2. I remove it to be more beautiful. If you can already do this only in CSS too, it’s even better than the gambiarra I did in Javascript.. hehe Thanks already @hugocsl =]

1 answer

3


It seems your system generates a span with the tag that actually stays outside the input and not inside it, this makes it a little difficult to do only with CSS, an alternative is to generate this span after the input to make it easier to tag if you have any interaction on input above.

Now let’s go to the code. The only JS I used, pure, was so you could focus on an input that is inside a div by clicking on the div, except it no longer needs JS for anything. So click on div pai seal on the input filho that is inside ok.

inserir a descrição da imagem aqui

In the image above, the span that will appear with the tag gets display:block, and will only appear if the input above have some value in. to make that validation I use :not(placeholder-shown), to know more read here Function - show password! I talk a little bit more about this.

There is a CSS property called :focus-within it serves to stylize an element if any child within it is focused. This is native to CSS and is with this pseudo-class that I put the color on div when foco in the input within it

And to "remove" the input I grant the technique assima with a :not(focus), gets like this input:not(:placeholder-shown):not(:focus). So if you don’t have placeholder pq logically has some value inside and is not focused, logically pq vc clicked out the input some.

To take out the input from the screen I used the same CSS that Bootstrap uses in the class sr-only, this CSS does not take away the input of the screen with display:none, because it hinders accessibility, sr-only just doesn’t let the input visible to "normal" users, but input still accessible to screen readers screen-Readers

Follow the code with the example. all with CSS (js só para focar o input quando clica na div, se vc precisar disso...)

var pai = document.querySelectorAll('.react-tagsinput');
var inp = document.querySelectorAll('.react-tagsinput-input');

function foca() {
	inp.forEach( (tag) => {
		tag.focus();
	})
}
pai.forEach( (el) => {
    el.addEventListener('click', foca);
})
* {
    box-sizing: border-box;
}
.react-tagsinput {
  padding: 20px;
  border: 1px solid #000;
}

.react-tagsinput:focus-within {
  background-color: tomato;
}
.react-tagsinput:focus-within .react-tagsinput-input {
  height: 50px;
}
.react-tagsinput .react-tagsinput-input + span {
  display: none;
}
.react-tagsinput .react-tagsinput-input:not(:placeholder-shown) + span {
  display: block;
}
.react-tagsinput .react-tagsinput-input:not(:placeholder-shown):not(:focus){
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0,0,0,0);
  border: 0;
}
<div class="react-tagsinput">
    <input type="text" class="react-tagsinput-input" placeholder="com placeholder">
    <span class="react-tagsinput-tag">tag</span>
</div>

  • 1

    Thanks so much for the help! No wonder your CSS reputation here in the stack is top! haha About the JS you put, as I’m using a ready theme, probably the guy already did it, because by clicking on the div already works the input normally. Man, thank you so much! =]

  • 1

    @Gleidnrique cool that worked out there young man! If the script of the theme already solves it is better not to use what I left in the reply, it was but to be able to assemble the same example. And keep in mind that these CSS properties are pretty new, and may not work in older browsers... especially in IE...[] s

Browser other questions tagged

You are not signed in. Login or sign up in order to post.