Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Outlined TextField's label has a different background color #82

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/lib/forms/TextField.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,7 @@
top: 1rem;
color: rgb(var(--error, var(--m3-scheme-on-surface-variant)));
pointer-events: none;
transition:
all 200ms,
font-size 300ms,
line-height 300ms,
letter-spacing 300ms;
transition: all 200ms, font-size 300ms, line-height 300ms, letter-spacing 300ms;
}
.layer {
position: absolute;
Expand All @@ -91,10 +87,11 @@
display: block;
width: 100%;
bottom: 0;

height: 0.0625rem;
background-color: rgb(var(--error, var(--m3-scheme-on-surface-variant)));
transition: all 200ms;
transition: all 250ms;
transform: scaleX(0);
transform-origin: center;
}
.m3-container :global(svg) {
width: 1.5rem;
Expand Down Expand Up @@ -145,6 +142,7 @@
input:focus ~ .layer::after {
height: 0.125rem;
background-color: rgb(var(--error, var(--m3-scheme-primary)));
transform: scaleX(1);
}
@media (hover: hover) {
button:hover {
Expand Down
29 changes: 22 additions & 7 deletions src/lib/forms/TextFieldOutlined.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@
export let value = "";
const dispatch = createEventDispatcher();
const id = crypto.randomUUID();

// find the nearest ancestor which has background color
function findNearestBackgroundColor(node: HTMLElement | null): string {
if (!node) {
return "rgb(var(--m3-util-background, var(--m3-scheme-surface)))";
}
const backgroundColor = window.getComputedStyle(node).backgroundColor;
if (backgroundColor === "rgba(0, 0, 0, 0)" || backgroundColor === "transparent") {
return findNearestBackgroundColor(node.parentElement);
}
return backgroundColor;
}

let labelElement: HTMLLabelElement;
$: labelBackgroundColor = findNearestBackgroundColor(labelElement?.parentElement);
</script>

<div
Expand All @@ -37,7 +52,12 @@
{...extraOptions}
/>
<div class="layer" />
<label class="m3-font-body-large" for={id}>{name}</label>
<label
class="m3-font-body-large"
for={id}
bind:this={labelElement}
style={`background-color: ${labelBackgroundColor}`}>{name}</label
>
{#if leadingIcon}
<Icon icon={leadingIcon} class="leading" />
{/if}
Expand Down Expand Up @@ -72,14 +92,9 @@
left: 0.75rem;
top: 1rem;
color: rgb(var(--error, var(--m3-scheme-on-surface-variant)));
background-color: rgb(var(--m3-util-background, var(--m3-scheme-surface)));
padding: 0 0.25rem;
pointer-events: none;
transition:
all 200ms,
font-size 300ms,
line-height 300ms,
letter-spacing 300ms;
transition: all 200ms, font-size 300ms, line-height 300ms, letter-spacing 300ms;
}
.layer {
position: absolute;
Expand Down
29 changes: 22 additions & 7 deletions src/lib/forms/TextFieldOutlinedMultiline.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@
},
};
};

// find the nearest ancestor which has background color
function findNearestBackgroundColor(node: HTMLElement | null): string {
if (!node) {
return "rgb(var(--m3-util-background, var(--m3-scheme-surface)))";
}
const backgroundColor = window.getComputedStyle(node).backgroundColor;
if (backgroundColor === "rgba(0, 0, 0, 0)" || backgroundColor === "transparent") {
return findNearestBackgroundColor(node.parentElement);
}
return backgroundColor;
}

let labelElement: HTMLLabelElement;
$: labelBackgroundColor = findNearestBackgroundColor(labelElement?.parentElement);
</script>

<div
Expand All @@ -48,7 +63,12 @@
{...extraOptions}
/>
<div class="layer" />
<label class="m3-font-body-large" for={id}>{name}</label>
<label
class="m3-font-body-large"
for={id}
bind:this={labelElement}
style={`background-color: ${labelBackgroundColor}`}>{name}</label
>
{#if leadingIcon}
<Icon icon={leadingIcon} />
{/if}
Expand Down Expand Up @@ -79,14 +99,9 @@
left: 0.75rem;
top: 1rem;
color: rgb(var(--error, var(--m3-scheme-on-surface-variant)));
background-color: rgb(var(--m3-util-background, var(--m3-scheme-surface)));
padding: 0 0.25rem;
pointer-events: none;
transition:
all 200ms,
font-size 300ms,
line-height 300ms,
letter-spacing 300ms;
transition: all 200ms, font-size 300ms, line-height 300ms, letter-spacing 300ms;
}
.layer {
position: absolute;
Expand Down