Skip to content

Instantly share code, notes, and snippets.

@tsmd
Last active May 30, 2019 15:10
Show Gist options
  • Select an option

  • Save tsmd/ced9fcd31245caed9bc078c491368a26 to your computer and use it in GitHub Desktop.

Select an option

Save tsmd/ced9fcd31245caed9bc078c491368a26 to your computer and use it in GitHub Desktop.
Cool textarea
@charset "UTF-8";
.Textarea {
@at-root {
& {
position: relative;
color: inherit;
font-size: rem(16px);
line-height: 1.8;
}
.Textarea__dummy {
display: none;
overflow: hidden;
visibility: hidden;
min-height: rem(120px);
padding: rem(4px) rem(14px);
white-space: pre-wrap;
word-wrap: break-word;
overflow-wrap: break-word;
border: 1px solid;
}
.Textarea__textarea {
display: block;
width: 100%;
height: rem(120px);
padding: rem(4px) rem(14px);
border: 1px solid $color-gray30;
border-radius: 4px;
font: inherit;
line-height: inherit;
resize: vertical;
}
&.-flex {
.Textarea__dummy {
display: block;
}
.Textarea__textarea {
position: absolute;
top: 0;
right: 0;
left: 0;
//height: auto;
height: 100%;
overflow: hidden;
resize: none;
}
}
// ------------------------------------------------------
// States
//
.Textarea__textarea:invalid {
outline: 0; // ブラウザ規定の invalid スタイルを打ち消し
box-shadow: none; // ブラウザ規定の invalid スタイルを打ち消し
.-invalid & {
border: 1px solid $color-alert;
}
}
&.-invalid {
.Textarea__textarea {
border: 1px solid $color-alert;
}
}
.Textarea__textarea:disabled {
color: $color-gray40;
border: 0;
background-color: $color-gray10;
}
.Textarea__textarea:focus {
background-color: #fff;
box-shadow: 0 0 0 4px $color-primary-10;
outline: 0;
}
}
}
import { Controller } from 'stimulus'
/**
* 内容物に応じて大きさが変わる Textarea
*/
export default class extends Controller {
static get targets() {
return ['dummy']
}
update(e) {
this.dummyTarget.textContent = e.target.value + '\u200b'
}
}
<!DOCTYPE html>
<html lang="ja">
<title>TextInput</title>
<div class="Textarea">
<textarea class="Textarea__textarea"></textarea>
</div>
<div class="Textarea -flex" data-controller="flex-textarea">
<div class="Textarea__dummy" aria-hidden="true" data-target="flex-textarea.dummy"></div>
<textarea class="Textarea__textarea" data-action="input->flex-textarea#update"></textarea>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment