Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
712e6f2d
Commit
712e6f2d
authored
Dec 13, 2019
by
Lyza Danger Gardner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add `ActionButton` component
parent
9eb89202
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
155 additions
and
6 deletions
+155
-6
action-button.js
src/sidebar/components/action-button.js
+63
-0
action-button-test.js
src/sidebar/components/test/action-button-test.js
+67
-0
buttons.scss
src/styles/mixins/buttons.scss
+25
-2
action-button.scss
src/styles/sidebar/components/action-button.scss
+0
-4
No files found.
src/sidebar/components/action-button.js
0 → 100644
View file @
712e6f2d
'use strict'
;
const
classnames
=
require
(
'classnames'
);
const
propTypes
=
require
(
'prop-types'
);
const
{
createElement
}
=
require
(
'preact'
);
const
SvgIcon
=
require
(
'./svg-icon'
);
/**
* A button, with (required) text label and an (optional) icon
*/
function
ActionButton
({
icon
=
''
,
isActive
=
false
,
isPrimary
=
false
,
label
,
onClick
=
()
=>
null
,
useCompactStyle
=
false
,
})
{
return
(
<
button
className
=
{
classnames
(
'action-button'
,
{
'action-button--compact'
:
useCompactStyle
,
'action-button--primary'
:
isPrimary
,
'is-active'
:
isActive
,
})}
onClick
=
{
onClick
}
aria
-
pressed
=
{
isActive
}
title
=
{
label
}
>
{
icon
&&
(
<
SvgIcon
name
=
{
icon
}
className
=
{
classnames
(
'action-button__icon'
,
{
'action-button__icon--compact'
:
useCompactStyle
,
'action-button__icon--primary'
:
isPrimary
,
})}
/
>
)}
<
span
className
=
"action-button__label"
>
{
label
}
<
/span
>
<
/button
>
);
}
ActionButton
.
propTypes
=
{
/** The name of the SVGIcon to render */
icon
:
propTypes
.
string
,
/** Is this button currently in an "active" or in an "on" state? */
isActive
:
propTypes
.
bool
,
/**
* Does this button represent the "primary" action available? If so,
* differentiating styles will be applied.
*/
isPrimary
:
propTypes
.
bool
,
/** a label used for the `title` and `aria-label` attributes */
label
:
propTypes
.
string
.
isRequired
,
/** callback for button clicks */
onClick
:
propTypes
.
func
,
/** Allows a variant of this type of button that takes up less space */
useCompactStyle
:
propTypes
.
bool
,
};
module
.
exports
=
ActionButton
;
src/sidebar/components/test/action-button-test.js
0 → 100644
View file @
712e6f2d
'use strict'
;
const
{
createElement
}
=
require
(
'preact'
);
const
{
mount
}
=
require
(
'enzyme'
);
const
ActionButton
=
require
(
'../action-button'
);
const
mockImportedComponents
=
require
(
'./mock-imported-components'
);
describe
(
'ActionButton'
,
()
=>
{
let
fakeOnClick
;
function
createComponent
(
props
=
{})
{
return
mount
(
<
ActionButton
icon
=
"fakeIcon"
label
=
"My Action"
onClick
=
{
fakeOnClick
}
{...
props
}
/
>
);
}
beforeEach
(()
=>
{
fakeOnClick
=
sinon
.
stub
();
ActionButton
.
$imports
.
$mock
(
mockImportedComponents
());
});
afterEach
(()
=>
{
ActionButton
.
$imports
.
$restore
();
});
it
(
'renders `SvgIcon` if icon property set'
,
()
=>
{
const
wrapper
=
createComponent
();
assert
.
equal
(
wrapper
.
find
(
'SvgIcon'
).
prop
(
'name'
),
'fakeIcon'
);
});
it
(
'does not render `SvgIcon` if no icon property set'
,
()
=>
{
const
wrapper
=
createComponent
({
icon
:
undefined
});
assert
.
isFalse
(
wrapper
.
find
(
'SvgIcon'
).
exists
());
});
it
(
'invokes `onClick` callback when pressed'
,
()
=>
{
const
wrapper
=
createComponent
();
wrapper
.
find
(
'button'
).
simulate
(
'click'
);
assert
.
calledOnce
(
fakeOnClick
);
});
it
(
'adds compact styles if `useCompactStyle` is `true`'
,
()
=>
{
const
wrapper
=
createComponent
({
useCompactStyle
:
true
});
assert
.
isTrue
(
wrapper
.
find
(
'button'
).
hasClass
(
'action-button--compact'
));
});
context
(
'in active state (`isActive` is `true`)'
,
()
=>
{
it
(
'adds `is-active` className'
,
()
=>
{
const
wrapper
=
createComponent
({
isActive
:
true
});
assert
.
isTrue
(
wrapper
.
find
(
'button'
).
hasClass
(
'is-active'
));
});
it
(
'sets `aria-pressed` attribute on button'
,
()
=>
{
const
wrapper
=
createComponent
({
isActive
:
true
});
assert
.
isTrue
(
wrapper
.
find
(
'button'
).
prop
(
'aria-pressed'
));
});
});
});
src/styles/mixins/buttons.scss
View file @
712e6f2d
...
...
@@ -43,13 +43,36 @@
color
:
var
.
$grey-5
;
font-weight
:
700
;
&
:hover
{
background-color
:
var
.
$grey-2
;
}
&
__label
{
padding-left
:
0
.25em
;
padding-right
:
0
.25em
;
}
&
__icon
{
color
:
var
.
$grey-5
;
margin
:
$icon-margin
;
&
--compact
{
margin
:
0
;
}
&
--primary
{
color
:
var
.
$grey-1
;
}
}
&
:hover
{
background-color
:
var
.
$grey-2
;
&
--primary
{
color
:
var
.
$grey-1
;
background-color
:
var
.
$grey-mid
;
&
:hover
{
color
:
var
.
$grey-1
;
background-color
:
var
.
$grey-6
;
}
}
}
...
...
src/styles/sidebar/components/action-button.scss
View file @
712e6f2d
...
...
@@ -2,8 +2,4 @@
.action-button
{
@include
buttons
.
action-button
;
&
__icon--compact
{
margin
:
0
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment