-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTextArea.svelte
145 lines (121 loc) · 3.76 KB
/
TextArea.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<!--
Copyright © 2022 The Radicle Design System Contributors
This file is part of radicle-design-system, distributed under the GPLv3
with Radicle Linking Exception. For full terms see the included
LICENSE file.
-->
<script lang="ts">
import type { TextInputValidationState } from "./TextInput";
export let resizable: boolean = false;
export let caption: string | undefined = undefined;
export let textareaStyle: string | undefined = undefined;
export let value: string | number | undefined = undefined;
export let placeholder: string | undefined = undefined;
export let validationState: TextInputValidationState = {
type: "unvalidated",
};
let textareaElement: HTMLTextAreaElement | undefined = undefined;
// We either auto-grow the text area, or allow the user to resize it. These
// options are mutually exclusive because a user resized textarea would
// automatically shrink upon text input otherwise.
$: if (textareaElement && !resizable) {
// React to changes to the textarea content.
value;
// Reset height to 0px on every value change so that the textarea
// immediately shrinks when all text is deleted.
textareaElement.style.height = `0px`;
textareaElement.style.height = `${textareaElement.scrollHeight}px`;
}
</script>
<style>
.container {
display: flex;
flex-direction: column;
width: 100%;
}
textarea {
background-color: var(--color-background);
border-radius: 0.5rem;
border: 1px solid var(--color-foreground-level-3);
height: 2.5rem;
padding: 0.5rem 0.75rem;
width: 100%;
min-height: 2.5rem;
resize: none;
}
.resizable {
resize: vertical;
}
textarea::-webkit-scrollbar {
display: initial;
}
textarea::-webkit-scrollbar-corner {
background-color: transparent;
}
textarea::-webkit-resizer {
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAMAAAAolt3jAAAAAXNSR0IB2cksfwAAAAlwSFlzAAAWJQAAFiUBSVIk8AAAAD9QTFRFAAAAZWZmZmZmZmVmZWVmwsLBwsLCZ2ZmwsPCZmdlZWZnwcLBZmZkYGJjw8LDwsPBZmZnZWZkZ2ZkwMDBWFtcNbXb2AAAABV0Uk5TAP///////////////////////1H/YDRrSAAAAFBJREFUeJxVjUESgCAMA2mqAoqK6P/f6kzjIXIos5NumpI8g5LbpJnNQvDl52mWUYTquqnXwstshpHaTi+o+hHXccoKmHVW9yvIxv218ntivmOYAWpLfqaRAAAAAElFTkSuQmCC);
background-size: 7px;
background-repeat: no-repeat;
background-position: bottom 1px right 1px;
}
textarea::placeholder {
color: var(--color-foreground-level-5);
}
textarea:focus,
textarea:hover {
background-color: var(--color-foreground-level-1);
border: 1px solid var(--color-foreground-level-3);
outline: none;
}
textarea.invalid:focus,
textarea.invalid {
background-position: right 0.875rem top 55%;
background: var(--color-background);
border: 1px solid var(--color-negative);
outline: none;
}
textarea.invalid:focus {
background: var(--color-foreground-level-1);
}
.validation-message {
align-items: center;
color: var(--color-negative);
display: flex;
margin-left: 0.75rem;
margin-top: 0.75rem;
text-align: left;
}
.caption {
align-items: center;
color: var(--color-foreground-level-4);
display: flex;
margin-left: 0.75rem;
margin-top: 0.75rem;
text-align: left;
}
</style>
<div class="container">
<textarea
style={textareaStyle}
bind:this={textareaElement}
bind:value
class:invalid={validationState.type === "invalid"}
class="wrap"
class:resizable
{placeholder}
on:change
on:click
on:input
on:keydown
on:keypress />
{#if caption && validationState.type !== "invalid"}
<div class="caption typo-text-small">
{caption}
</div>
{/if}
{#if validationState.type === "invalid"}
<div class="validation-message">
{validationState.message}
</div>
{/if}
</div>