Svelte Twitter Tweet Box
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script> | |
let message = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit'; | |
let messageCounter; | |
let buttonState; | |
let counterState; | |
$: message; | |
$: messageCounter = calcCounter(message); | |
$: buttonState = getButtonState(messageCounter); | |
$: counterState = getCounterState(messageCounter); | |
function calcCounter(message) { | |
return 280 - message.length | |
} | |
function getButtonState(messageCounter) { | |
return messageCounter >= 0 && messageCounter <= 140 ? 'active' : ''; | |
} | |
function getCounterState (messageCounter) { | |
if (messageCounter > 10 && messageCounter < 21) { | |
return 'warning'; | |
} else if (messageCounter < 11) { | |
return 'danger'; | |
} | |
return ''; | |
} | |
</script> | |
<div class="tweet-box"> | |
<textarea class="tweet-input" bind:value={message}></textarea> | |
<button class="{ buttonState }" disabled="{ messageCounter < 0 }">Tweet</button> | |
<span class="{ counterState }">{ messageCounter }</span> | |
</div> | |
<style> | |
.tweet-box { | |
font-size: 18px; | |
padding: 10px; | |
position: relative; | |
} | |
.tweet-input { | |
border: 1px solid #ccc; | |
border-radius: 10px; | |
box-sizing: border-box; | |
font-family: sans-serif; | |
font-size: 18px; | |
padding: 15px 12px; | |
outline: none; | |
width: 100%; | |
height: 9em; | |
} | |
span { | |
float: right; | |
padding: 18px 14px; | |
} | |
.warning { | |
color: #a76c00; | |
} | |
.danger { | |
color: #800000; | |
} | |
button { | |
background-color: #ccc; | |
border: 1px solid #eee; | |
border-radius: 10px; | |
color: #fefefe; | |
font-size: 18px; | |
float: right; | |
margin-top: 10px; | |
padding: 10px 25px; | |
} | |
button.active { | |
background-color: #008cff; | |
cursor: pointer; | |
} | |
</style> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment