Skip to content

Commit

Permalink
refactor form validation mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
basher committed Nov 11, 2024
1 parent 2193a70 commit c97fc03
Showing 1 changed file with 23 additions and 27 deletions.
50 changes: 23 additions & 27 deletions ui/src/css/form/_form.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,6 @@ Dependencies.
@use '../mixins' as *;
@use '../base' as *;

@mixin validation {
:is(.label, .checkbox__label, .radio__label) {
padding-inline-end: var(--gutter-m);
position: relative;

&::after {
color: $color-error;
content: '!';
display: block;
font-size: 1em;
font-weight: bold;
inset-block-start: 0;
inset-inline-end: 0;
position: absolute;
}
}
}

.form {
display: flex;
flex-direction: column;
Expand All @@ -34,10 +16,29 @@ Dependencies.
flex-direction: column;
gap: var(--gutter-xs);

// Native HTML5 validation.
&:has(:valid:required) {
@include validation;
// Native HTML5 validation, and errors rendered directly in HTML, or via JavaScript.
&:has(:valid:required),
&:has(:user-invalid:required),
&--has-error {
:is(.label, .checkbox__label, .radio__label) {
padding-inline-end: var(--gutter-m);
position: relative;

/* stylelint-disable-next-line max-nesting-depth, no-descending-specificity */
&::after {
color: $color-error;
content: '!';
display: block;
font-size: 1em;
font-weight: bold;
inset-block-start: 0;
inset-inline-end: 0;
position: absolute;
}
}
}

&:has(:valid:required) {
:is(.label, .checkbox__label, .radio__label) {
/* stylelint-disable-next-line max-nesting-depth */
&::after {
Expand All @@ -46,10 +47,6 @@ Dependencies.
}
}
}

&:has(:user-invalid:required) {
@include validation;
}
}

// Errors rendered directly in HTML, or via JavaScript.
Expand All @@ -58,8 +55,6 @@ Dependencies.
}

&__field--has-error {
@include validation;

&:has(:valid) {
.form__error {
display: none;
Expand All @@ -68,6 +63,7 @@ Dependencies.
}
}

// Basic form component styles.
:is(.input, .select, .textarea) {
@include focus($background: true);
border: var(--border-width-s) solid;
Expand Down

0 comments on commit c97fc03

Please sign in to comment.