Skip to content

Commit

Permalink
Merge pull request #11 from lukasoppermann/styled-inputs
Browse files Browse the repository at this point in the history
Styled inputs
  • Loading branch information
lukasoppermann authored Mar 1, 2021
2 parents 41eb62f + 305d075 commit d0035e3
Show file tree
Hide file tree
Showing 8 changed files with 262 additions and 33 deletions.
96 changes: 81 additions & 15 deletions dist/ui.html

Large diffs are not rendered by default.

90 changes: 75 additions & 15 deletions dist/ui.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
"typescript-bundle": "^1.0.17",
"url-loader": "^4.1.1",
"webpack": "^4.38.0",
"webpack-cli": "^3.3.6",
"mathjs": "^9.2.0"
"webpack-cli": "^3.3.6"
},
"standardx": {
"parser": "@typescript-eslint/parser",
Expand Down
50 changes: 50 additions & 0 deletions src/ui/css/ui.css
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ footer a {
color: var(--dark-grey);
font-size: var(--font-size-small);
}
.input__field{
font-family: var(--font-stack);
}
.hidden {
display: none !important;
}
Expand Down Expand Up @@ -135,6 +138,53 @@ footer a {
height: var(--size-medium);
z-index: 1;
}
.input__overlay {
position: absolute;
pointer-events: none;
z-index: 10;
top: 0px;
left: 36px;
font-size: var(--font-size-xsmall);
font-weight: var(--font-weight-normal);
letter-spacing: var(--font-letter-spacing-neg-xsmall);
line-height: var(--line-height);
width: calc(100% - 38px);
height: 26px;
margin: 2px 0 2px 1px;
padding: calc(var(--size-xxsmall) - 1px) var(--size-xxxsmall) var(--size-xxsmall) 0;
color: var(--black8);
outline: none;
background-color: var(--white);
border-radius: 1px;
}
.input__overlay .hl_hash {
padding: 0 2px;
color: rgb(var(--blue-rgb));
font-weight: var(--font-weight-bold);
background: rgba(var(--blue-rgb), 0.1);
border-radius: 3px;
}
.input__overlay .hl_operator {
padding: 0 1px;
color: var(--medium-grey);
}
.input__overlay .hl_parenthese {
padding: 0 1px;
font-weight: var(--font-weight-bold);
color: var(--lighter-grey);
}
.input--with-text-medium .input__overlay{
width: calc(100% - 82px);
left: 80px;
}
.input--with-text .input__overlay{
width: calc(100% - 57px);
left: 55px;
}
.input--with-icon .input__overlay{
width: calc(100% - 34px);
left: 32px;
}
input:invalid {
border: 1px solid var(--red);
outline: 1px solid var(--red);
Expand Down
4 changes: 3 additions & 1 deletion src/ui/css/variables.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
/* COLORS */
/* Accent */
--red: #f24822;
--blue-rgb: 24, 160, 251;
--dark-grey: var(--black8);
--medium-grey: var(--black3);
--medium-grey: rgba(0, 0, 0, .5);
--lighter-grey: rgba(0, 0, 0, .3);
--light-grey: var(--silver);
--white-grey: #f9f9f9;
/* Font sizes */
Expand Down
41 changes: 41 additions & 0 deletions src/ui/modules/styledInputs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export default (form: HTMLFormElement) => {
const inputs: HTMLInputElement[] = Array.from(form.querySelectorAll('input'))
// repare value for highlighting
const prepareValue = (value: string) => {
// highlight operators
value = value.replaceAll(/[\*\+-]/gi, '<span class="hl_operator">$&</span>')
// slash operator
value = value.replaceAll(/(?<!\<)\//gi, '<span class="hl_operator">$&</span>')
// parenthese
value = value.replaceAll(/[\(\)]/gi, '<span class="hl_parenthese">$&</span>')
// highlight #
value = value.replaceAll('#', '<span class="hl_hash">#</span>')
// return value
return value
}
// leave form field
const onBlur = (event) => {
const input: HTMLInputElement = event.target as HTMLInputElement
const overlay = input.parentNode.querySelector('.input__overlay')
if (overlay !== null) {
overlay.innerHTML = prepareValue(input.value) || ''
overlay.classList.remove('hidden')
}
}
// leave form field
const onFocus = (event: Event) => {
const input: HTMLInputElement = event.target as HTMLInputElement
const overlay = input.parentNode.querySelector('.input__overlay')
if (overlay !== null) {
overlay.classList.add('hidden')
}
}
// bind events
inputs.forEach(input => {
onBlur({ target: input })
// blur
input.addEventListener('blur', onBlur)
// focus
input.addEventListener('focus', onFocus)
})
}
6 changes: 6 additions & 0 deletions src/ui/ui.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</div>
<div class="input input--with-icon input--style-name" data-tooltip="Name for the effect styles. Use # for the current step.">
<div class="icon icon--effects"></div>
<div class="input__overlay"></div>
<input class="input__field" data-id="styleName" type="text" placeholder="Style name (e.g. Elevation/# dp)">
</div>
<div class="section-title">Elevation Layers (added to each step)<a class="icon icon--questionmark" href="https://github.com/lukasoppermann/elevation-scale/blob/main/README.md#adding-layers" target="_blank"></a></div>
Expand Down Expand Up @@ -48,20 +49,24 @@
<div class="row">
<div class="input input--with-text-small">
<div class="label">X</div>
<div class="input__overlay"></div>
<input class="input__field" data-property="x" type="text" required pattern="^[0-9#\.\-\*\+\/\(\)]{1,}$" placeholder="x value" value=".25*#">
</div>
<div class="input input--with-text">
<div class="label">Blur</div>
<div class="input__overlay"></div>
<input class="input__field" data-property="radius" type="text" required pattern="^[0-9#\.\-\*\+\/\(\)]{1,}$" placeholder="blur value" value="2*#">
</div>
</div>
<div class="row">
<div class="input input--with-text-small">
<div class="label">Y</div>
<div class="input__overlay"></div>
<input class="input__field" data-property="y" type="text" required pattern="^[0-9#\.\-\*\+\/\(\)]{1,}$" placeholder="y value" value="1.5*#">
</div>
<div class="input input--with-text">
<div class="label">Spread</div>
<div class="input__overlay"></div>
<input class="input__field" data-property="spread" type="text" required pattern="^[0-9#\.\-\*\+\/\(\)]{1,}$" placeholder="spread value" value="0">
</div>
</div>
Expand All @@ -72,6 +77,7 @@
</div>
<div class="input input--with-text">
<div class="label">Opacity</div>
<div class="input__overlay"></div>
<input class="input__field" data-property="opacity" type="text" required pattern="^[0-9#\.\-\*\+\/\(\)]{1,}$" placeholder="opacity in %" value="10">
</div>
</div>
Expand Down
5 changes: 5 additions & 0 deletions src/ui/ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import createElevationLayer from './modules/createElevationLayer'
import addElevationLayer from './modules/addElevationLayer'
import toggleDeleteButton from './modules/toggleDeleteButton'
import limitToAllowed from './modules/limitToAllowed'
import styledInputs from './modules/styledInputs'
import config from './modules/config'
import setColorPreview from './modules/setColorPreview'

Expand Down Expand Up @@ -54,6 +55,8 @@ const updatePanel = data => {
toggleElevationLayer(list.querySelector(`[data-id="${config.ids.elevationLayer}"]`))
// limit inputs
limitToAllowed()
// actvate styles inputs
styledInputs(form)
}

const toggleEmptyState = active => {
Expand Down Expand Up @@ -106,6 +109,8 @@ createScaleButton.addEventListener('click', () => {

document.querySelector('[data-id="add"').addEventListener('click', (event) => {
addElevationLayer(list)
// update inputs
styledInputs(form)
// update if valid form
if (form.checkValidity() === true) {
postUpdateElevation(form)
Expand Down

0 comments on commit d0035e3

Please sign in to comment.