Refactor Button component

This commit is contained in:
Justin Edmund 2022-03-04 00:16:53 -08:00
parent fde068dab5
commit be60319f98
6 changed files with 176 additions and 74 deletions

View file

@ -67,9 +67,9 @@ const BottomHeader = () => {
const leftNav = () => { const leftNav = () => {
if (router.pathname === '/p/[party]' || router.pathname === '/new') { if (router.pathname === '/p/[party]' || router.pathname === '/new') {
if (app.party.detailsVisible) { if (app.party.detailsVisible) {
return (<Button icon="edit" active={true} click={toggleDetails}>Hide info</Button>) return (<Button icon="edit" active={true} onClick={toggleDetails}>Hide info</Button>)
} else { } else {
return (<Button icon="edit" click={toggleDetails}>Edit info</Button>) return (<Button icon="edit" onClick={toggleDetails}>Edit info</Button>)
} }
} else { } else {
return (<div />) return (<div />)

View file

@ -134,6 +134,71 @@
color: #bababa; color: #bababa;
} }
} }
&.null {
background: $grey-90;
color: $grey-50;
&:hover {
background: $grey-70;
color: $grey-00;
}
}
&.wind {
background: $wind-bg-light;
color: $wind-text-dark;
&:hover {
background: darken($wind-bg-light, 10);
}
}
&.fire {
background: $fire-bg-light;
color: $fire-text-dark;
&:hover {
background: darken($fire-bg-light, 10);
}
}
&.water {
background: $water-bg-light;
color: $water-text-dark;
&:hover {
background: darken($water-bg-light, 10);
}
}
&.earth {
background: $earth-bg-light;
color: $earth-text-dark;
&:hover {
background: darken($earth-bg-light, 10);
}
}
&.dark {
background: $dark-bg-light;
color: $dark-text-dark;
&:hover {
background: darken($dark-bg-light, 10);
}
}
&.light {
background: $light-bg-light;
color: $light-text-dark;
&:hover {
background: darken($light-bg-light, 10);
}
}
.text { .text {
color: inherit; color: inherit;

View file

@ -1,4 +1,4 @@
import React from 'react' import React, { useEffect, useState } from 'react'
import classNames from 'classnames' import classNames from 'classnames'
import Link from 'next/link' import Link from 'next/link'
@ -16,80 +16,118 @@ import './index.scss'
import { ButtonType } from '~utils/enums' import { ButtonType } from '~utils/enums'
interface Props { interface Props {
active: boolean active?: boolean
disabled: boolean disabled?: boolean
icon: string | null classes?: string[],
type: ButtonType icon?: string
click: any type?: ButtonType
children?: React.ReactNode
onClick?: (event: React.MouseEvent<HTMLElement>) => void
} }
interface State { const Button = (props: Props) => {
isPressed: boolean // States
} const [active, setActive] = useState(false)
const [disabled, setDisabled] = useState(false)
const [pressed, setPressed] = useState(false)
const [buttonType, setButtonType] = useState(ButtonType.Base)
class Button extends React.Component<Props, State> { const classes = classNames({
static defaultProps: Props = { Button: true,
active: false, 'Active': active,
disabled: false, 'btn-pressed': pressed,
icon: null, 'btn-disabled': disabled,
type: ButtonType.Base, 'save': props.icon === 'save',
click: () => {} 'destructive': props.type == ButtonType.Destructive
} }, props.classes)
constructor(props: Props) { useEffect(() => {
super(props) if (props.active) setActive(props.active)
this.state = { if (props.disabled) setDisabled(props.disabled)
isPressed: false, if (props.type) setButtonType(props.type)
} }, [props.active, props.disabled, props.type])
}
render() { const addIcon = (
let icon <span className='icon'>
if (this.props.icon === 'new') { <AddIcon />
icon = <span className='icon'> </span>
<AddIcon /> )
</span>
} else if (this.props.icon === 'menu') { const menuIcon = (
icon = <span className='icon'> <span className='icon'>
<MenuIcon /> <MenuIcon />
</span> </span>
} else if (this.props.icon === 'link') { )
icon = <span className='icon stroke'>
<LinkIcon /> const linkIcon = (
</span> <span className='icon stroke'>
} else if (this.props.icon === 'cross') { <LinkIcon />
icon = <span className='icon'> </span>
<CrossIcon /> )
</span>
} else if (this.props.icon === 'edit') { const crossIcon = (
icon = <span className='icon'> <span className='icon'>
<EditIcon /> <CrossIcon />
</span> </span>
} else if (this.props.icon === 'save') { )
icon = <span className='icon stroke'>
<SaveIcon /> const editIcon = (
</span> <span className='icon'>
} else if (this.props.icon === 'settings') { <EditIcon />
icon = <span className='icon settings'> </span>
<SettingsIcon /> )
</span>
const saveIcon = (
<span className='icon stroke'>
<SaveIcon />
</span>
)
const settingsIcon = (
<span className='icon settings'>
<SettingsIcon />
</span>
)
function getIcon() {
let icon: React.ReactNode
switch(props.icon) {
case 'new': icon = addIcon; break
case 'menu': icon = menuIcon; break
case 'link': icon = linkIcon; break
case 'cross': icon = crossIcon; break
case 'edit': icon = editIcon; break
case 'save': icon = saveIcon; break
case 'settings': icon = settingsIcon; break
} }
const classes = classNames({ return icon
Button: true, }
'Active': this.props.active,
'btn-pressed': this.state.isPressed,
'btn-disabled': this.props.disabled,
'save': this.props.icon === 'save',
'destructive': this.props.type == ButtonType.Destructive
})
return <button className={classes} disabled={this.props.disabled} onClick={this.props.click}> function handleMouseDown() {
{icon} setPressed(true)
{ (this.props.type != ButtonType.IconOnly) ? }
<span className='text'>{this.props.children}</span> : '' }
function handleMouseUp() {
setPressed(false)
}
return (
<button
className={classes}
disabled={disabled}
onMouseDown={handleMouseDown}
onMouseUp={handleMouseUp}
onClick={props.onClick}>
{ getIcon() }
{ (props.type != ButtonType.IconOnly) ?
<span className='text'>
{ props.children }
</span> : ''
}
</button> </button>
} )
} }
export default Button export default Button

View file

@ -115,7 +115,7 @@ const GridRep = (props: Props) => {
active={props.favorited} active={props.favorited}
icon="save" icon="save"
type={ButtonType.IconOnly} type={ButtonType.IconOnly}
click={sendSaveData} onClick={sendSaveData}
/> : ''} /> : ''}
</div> </div>
<div className="bottom"> <div className="bottom">

View file

@ -107,9 +107,9 @@ const TopHeader = () => {
const saveButton = () => { const saveButton = () => {
if (party.favorited) if (party.favorited)
return (<Button icon="save" active={true} click={toggleFavorite}>Saved</Button>) return (<Button icon="save" active={true} onClick={toggleFavorite}>Saved</Button>)
else else
return (<Button icon="save" click={toggleFavorite}>Save</Button>) return (<Button icon="save" onClick={toggleFavorite}>Save</Button>)
} }
const rightNav = () => { const rightNav = () => {
@ -119,9 +119,9 @@ const TopHeader = () => {
saveButton() : '' saveButton() : ''
} }
{ (router.route === '/p/[party]') ? { (router.route === '/p/[party]') ?
<Button icon="link" click={copyToClipboard}>Copy link</Button> : '' <Button icon="link" onClick={copyToClipboard}>Copy link</Button> : ''
} }
<Button icon="new" click={newParty}>New</Button> <Button icon="new" onClick={newParty}>New</Button>
</div> </div>
) )
} }

View file

@ -14,7 +14,6 @@ import { appState } from '~utils/appState'
import CrossIcon from '~public/icons/Cross.svg' import CrossIcon from '~public/icons/Cross.svg'
import './index.scss' import './index.scss'
interface GridWeaponObject { interface GridWeaponObject {
weapon: { weapon: {
element?: number element?: number