From 8106adf35eb5ba321ecebf44f11534c530d19b30 Mon Sep 17 00:00:00 2001
From: Yoshua Wuyts
Date: Thu, 2 Mar 2017 15:03:45 +0100
Subject: [PATCH] Buttonify (#282)
* implement button component
* fixup! add newButton
* add newIcon, newButton pkgs
* update icon
* fixup! more progress
* fixup! migrate main.js
* fixup! all aboard the button train
* fixup! move icons over
* fixup! restore file names
* fixup! fix tests
* fixup! fix missing buttons
* fix final button styles
* fix alignment of network icon
* fix alignment of checkmark icon in copy link modal
* remove color variables from default icon styles
* fix size and alignment of intro screen svgs
---
elements/button.js | 166 +++++++++++++++++++++++++++++---------
elements/confirm-modal.js | 18 ++---
elements/crash-modal.js | 24 +++---
elements/dat-import.js | 7 +-
elements/error-modal.js | 8 +-
elements/header.js | 23 +++---
elements/icon.js | 26 ++++--
elements/link-modal.js | 9 ++-
elements/table.js | 46 +++++------
package.json | 3 +-
pages/main.js | 43 +++++-----
11 files changed, 222 insertions(+), 151 deletions(-)
diff --git a/elements/button.js b/elements/button.js
index 88d08a29..dc95f561 100644
--- a/elements/button.js
+++ b/elements/button.js
@@ -1,14 +1,15 @@
'use strict'
const html = require('choo/html')
+const assert = require('assert')
const css = require('sheetify')
-const icon = require('./icon')
+const xtend = require('xtend')
-const prefix = css`
+const baseStyles = css`
:host {
text-transform: uppercase;
letter-spacing: .025em;
- .btn-wrapper {
+ .btn-inner-wrapper {
display: flex;
flex-wrap: nowrap;
flex-direction: row;
@@ -17,71 +18,160 @@ const prefix = css`
}
}
.icon-only {
- .btn-text {
- display: none;
- }
+ .btn-text { display: none }
}
- .filled-green {
+`
+
+var greenStyles = css`
+ :host {
padding: .5rem .75rem;
font-size: .75rem;
background-color: var(--color-green);
color: var(--color-neutral-04);
}
- .filled-green:hover,
- .filled-green:focus {
+ :host:hover,
+ :host:focus {
background-color: var(--color-green-hover);
color: var(--color-white);
}
- .filled-red {
+`
+
+var redStyles = css`
+ :host {
padding: .5rem .75rem;
font-size: .75rem;
background-color: var(--color-red);
color: var(--color-neutral-04);
}
- .filled-red:hover,
- .filled-red:focus {
+ :host:hover,
+ :host:focus {
background-color: var(--color-red-hover);
color: var(--color-white);
}
- .plain {
+`
+
+var plainStyles = css`
+ :host {
padding: .5rem .75rem;
font-size: .75rem;
background-color: transparent;
color: var(--color-neutral-40);
}
- .plain:hover,
- .plain:focus {
+ :host:hover,
+ :host:focus {
color: var(--color-neutral-70);
}
`
-module.exports = (props, click) => {
- if (typeof click === 'function') props.click = click
-
- var child
- if (props.icon) {
- child = html`
-
- ${icon({
- id: props.icon
- })}
- ${props.text}
-
`
+plainButton.green = greenButton
+plainButton.icon = iconButton
+plainButton.red = redButton
+module.exports = plainButton
+
+// States:
+// - Text only
+// - Text and icon
+function buttonElement (innerText, opts) {
+ if (!opts) {
+ opts = innerText
+ innerText = ''
+ }
+
+ var icon = opts.icon
+ var innerHTML = null
+
+ if (innerText && !icon) {
+ innerHTML = html`
+
+ ${innerText}
+
+ `
} else {
- child = html`
-
- ${props.text}
-
`
+ innerHTML = html`
+
+ ${icon}
+ ${innerText}
+
+ `
+ }
+
+ var defaultProps = {
+ 'aria-label': innerText,
+ 'title': innerText
}
+ var buttonProps = xtend(defaultProps, opts)
+ buttonProps.class = 'pointer ' + baseStyles + ' ' + buttonProps.class
+
return html`
-
`
diff --git a/elements/crash-modal.js b/elements/crash-modal.js
index fc6afbb7..34b8cf4a 100644
--- a/elements/crash-modal.js
+++ b/elements/crash-modal.js
@@ -52,11 +52,9 @@ function createWidget () {
possible.
- ${button({
- text: 'Exit Application',
- style: 'filled-green',
- cls: 'fr ml3',
- click: onexit
+ ${button.green('Exit Application', {
+ class: 'fr ml3',
+ onclick: onexit
})}
@@ -67,17 +65,13 @@ function createWidget () {
be reversed.
- ${button({
- text: 'Clear Database & exit',
- style: 'filled-red',
- cls: 'fr ml3',
- click: clearDatabase
+ ${button.red('Clear Database & exit', {
+ class: 'fr ml3',
+ onclick: clearDatabase
})}
- ${button({
- text: 'Delete All Data & exit',
- style: 'filled-red',
- cls: 'fr ml3',
- click: deleteData
+ ${button.red('Delete All Data & exit', {
+ class: 'fr ml3',
+ onclick: deleteData
})}
diff --git a/elements/dat-import.js b/elements/dat-import.js
index 21830f9f..7cc239e5 100644
--- a/elements/dat-import.js
+++ b/elements/dat-import.js
@@ -64,11 +64,6 @@ function datImportElement (props) {
assert.equal(typeof onsubmit, 'function', 'dat-import: onsubmit should be type function')
- const linkIcon = icon({
- id: 'link',
- cls: 'absolute top-0 bottom-0 left-0'
- })
-
return html`
`
diff --git a/elements/error-modal.js b/elements/error-modal.js
index 0658083b..37ff4b94 100644
--- a/elements/error-modal.js
+++ b/elements/error-modal.js
@@ -35,11 +35,9 @@ function createWidget () {
})
function render (message, onexit) {
- var exitButton = button({
- text: 'Ok',
- style: 'filled-green',
- cls: 'fr ml3',
- click: onexit
+ var exitButton = button.green('Ok', {
+ class: 'fr ml3',
+ onclick: onexit
})
return html`
diff --git a/elements/header.js b/elements/header.js
index 0a7deb9b..bd1a58e6 100644
--- a/elements/header.js
+++ b/elements/header.js
@@ -3,8 +3,10 @@
const html = require('choo/html')
const assert = require('assert')
const css = require('sheetify')
+
const button = require('./button')
const datImport = require('./dat-import')
+const icon = require('./icon')
module.exports = headerElement
@@ -76,22 +78,17 @@ function headerElement (props) {
var importButton = datImport({ onsubmit: onimport })
- var createButton = button({
- icon: 'create-new-dat',
- text: 'Create New Dat',
- cls: 'ml2 b--transparent header-action header-action-no-border',
- click: oncreate
+ var createButton = button('Create New Dat', {
+ icon: icon('create-new-dat'),
+ class: 'ml2 b--transparent header-action header-action-no-border',
+ onclick: oncreate
})
- var loginButton = button({
- text: 'Log In',
- cls: 'ml2 header-action log-in-button'
- })
+ var loginButton = button('Log In', { class: 'ml2 header-action log-in-button' })
- var menuButton = button({
- icon: 'menu',
- text: '',
- cls: 'ml2 header-action header-action-no-border menu-trigger'
+ var menuButton = button.icon('Open Menu', {
+ icon: icon('menu'),
+ class: 'ml2 header-action header-action-no-border menu-trigger'
})
return html`
diff --git a/elements/icon.js b/elements/icon.js
index 265ef4f9..1ab2f6eb 100644
--- a/elements/icon.js
+++ b/elements/icon.js
@@ -1,16 +1,30 @@
'use strict'
const html = require('choo/html')
-const css = require('yo-css')
+const assert = require('assert')
+const css = require('sheetify')
-module.exports = (props) => {
- const style = {
- fill: 'currentColor'
+var prefix = css`
+ :host {
+ display: block;
+ fill: currentColor;
}
+`
+
+module.exports = iconElement
+
+function iconElement (iconName, opts) {
+ opts = opts || {}
+
+ assert.equal(typeof iconName, 'string', 'elements/icon: iconName should be type string')
+ assert.equal(typeof opts, 'object', 'elements/icon: opts should be type object')
+
+ var classNames = 'icon-' + iconName + ' ' + prefix
+ if (opts.class) classNames += (' ' + opts.class)
return html`
-