From 11dc309dcfbbda966348cdca452a3c9c65cd0914 Mon Sep 17 00:00:00 2001 From: Claas Date: Sat, 24 Jun 2023 17:58:31 +0200 Subject: [PATCH] Implement empty check without JavaScript --- src/dim-outlined-input.ts | 59 ++++++++++++++++++++++++++++----------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/src/dim-outlined-input.ts b/src/dim-outlined-input.ts index f788ac2..5510c48 100644 --- a/src/dim-outlined-input.ts +++ b/src/dim-outlined-input.ts @@ -13,7 +13,7 @@ export class DimOutlinedInput extends LitElement { autofocus: boolean = false; @property({ type: String }) - placeholder: string = ""; + placeholder: string = " "; #oninput(event: Event) { // Keep the value up to date to be read by external event listeners through the event.target.value property @@ -39,7 +39,6 @@ export class DimOutlinedInput extends LitElement { :placeholder-shown (not empty -> :not(:placeholder-shown) + - focus -> :focus-within + - hover -> :hover + - disabled -> :disabled - error - (- placeholder shown) + - placeholder -> :not([placeholder=" "]) (no placeholder -> [placeholder=" "]) (- supporting text) (- character count) (- character limit) (- leading icon) - (- trailing icon) */ - - /* EMPTY, FOCUS */ - /* Only remove border top when label moves up e.g focus, not-empty, placeholder shown */ - .group:has(input[empty]):focus-within, - /* NOT EMPTY, FOCUS */ - .group:has(input:not([empty])):focus-within { + (- trailing icon) + (- autofilled -> :autofill ) + (- AND ALL THE DIFFERENT TYPES OF INPUT ) */ + + /* NOT EMPTY, FOCUS, NO PLACEHOLDER */ + /* |-NO-PLACEHOLDER||-NOT-EMPTY------------| |-FOCUS-------| */ + .group:has(input[placeholder=" "]:not(:placeholder-shown)):focus-within, + /* NOT EMPTY, FOCUS, PLACEHOLDER */ + /* |-PLACEHOLDER--------| |-NOT-EMPTY------------| |-FOCUS-----| */ + .group:has(input:not([placeholder=" "]):not(:placeholder-shown)):focus-within, + /* EMPTY, FOCUS, PLACEHOLDER */ + /* |-PLACEHOLDER--------| |-EMPTY----------| |-FOCUS-------| */ + .group:has(input:not([placeholder=" "]):placeholder-shown):focus-within, + /* EMPTY, FOCUS, NO PLACEHOLDER */ + /* |-NO-PLACEHOLDER||-EMPTY----------| |-FOCUS-------| */ + .group:has(input[placeholder=" "]:placeholder-shown):focus-within { & input { color: var(--md-sys-color-primary); } @@ -234,8 +242,27 @@ export class DimOutlinedInput extends LitElement { } } + /* When placeholder is filled (:not([placeholder=" "])) and it is shown, and the it is empty, move label up */ + .group:has(input:not([placeholder=" "]):placeholder-shown) { + & .fillers { + & .filler-middle { + /* border-top-color: transparent; */ + padding-top: 2px; + border-width: 0 0 1px 0; + & label { + translate: 0 -50%; + padding: 0 4px; + font-size: var(--md-sys-typescale-body-small-font-size); + line-height: var(--md-sys-typescale-body-small-line-height); + } + } + } + } + + /* We check if input is empty with a dirty trick that looks for the placeholder not being shown when the placeholder + is a space character that looks to the user as if there was no placeholder */ /* NOT EMPTY, NOT FOCUS */ - .group:has(input:not([empty])):not(:focus-within) { + .group:has(input:not(:placeholder-shown)):not(:focus-within) { & .fillers .filler-middle { border-top-color: transparent; padding-top: 2px; -- knotmirror 0.0.1