11// Playground for Button
22
3+ const buttonVariants = [ 'default' , 'strong' , 'brand' , 'minimal' , 'negative' ]
4+ const layouts = [ 'Text only' , 'Text + Icon' , 'Icon only' ]
5+ const states = [ 'enabled' , 'disabled' , 'loading' ]
6+ const roundedOptions = [ false , true ]
7+
8+ const defaultIcon = '<path d="M851.8 172.224C754.746 75.18 597.77 74.613 500 170.492c-97.77-95.879-254.746-95.312-351.795 1.732-96.042 96.038-97.605 250.77-4.7 348.724L500 914v-.006.006l356.5-393.05c92.9-97.956 91.338-252.688-4.7-348.726Zm-65.476 277.332L500 766.694 213.677 449.557l-.145-.162a150.005 150.005 0 0 1 217.526-206.464q.222.222.45.438l43.788 41.64L500 308.784l24.7-23.77 43.794-41.645q.226-.216.449-.438A149.05 149.05 0 0 1 675.018 199a150 150 0 0 1 111.451 250.4Z" style="fill-rule:evenodd"/>'
9+
10+ const loadingLoader = `<svg viewBox='0 0 40 40' xmlns='http://www.w3.org/2000/svg' class="loader" aria-hidden="true">
11+ <circle class="loader-inner" cx="20" cy="20" r="17"></circle>
12+ </svg>`
13+
14+ const renderIcon = ( icon , hidden = false ) => {
15+ if ( ! icon ) {
16+ return ''
17+ }
18+
19+ return `<svg ${ hidden ? 'aria-hidden="true"' : '' } fill="currentColor" viewBox="0 0 1000 1000">${ icon } </svg>`
20+ }
21+
22+ const renderButton = ( { label, variant, layout, state, icon, rounded } ) => {
23+ const isIconOnly = layout === 'Icon only'
24+ const shouldRenderIcon = layout !== 'Text only'
25+ const isLoading = state === 'loading'
26+ const isDisabled = state === 'disabled'
27+ const classes = [ 'btn' ]
28+
29+ classes . push ( `btn-${ variant } ` )
30+
31+ if ( isIconOnly ) {
32+ classes . push ( 'btn-icon' )
33+ }
34+
35+ if ( isLoading ) {
36+ classes . push ( 'loading-indeterminate' )
37+ }
38+
39+ const iconMarkup = renderIcon ( icon || defaultIcon , true )
40+ const loadingMarkup = isLoading
41+ ? `${ loadingLoader }
42+ <span role="status" class="visually-hidden">Loading ${ label } </span>`
43+ : ''
44+
45+ const buttonMarkup = `<button type="button" class="${ classes . join ( ' ' ) } "${ isDisabled || isLoading ? ' disabled' : '' } >
46+ ${ shouldRenderIcon ? iconMarkup : '' }
47+ ${ isIconOnly ? `<span class="visually-hidden">${ label } </span>` : label }
48+ ${ loadingMarkup }
49+ </button>`
50+
51+ if ( rounded ) {
52+ return `<div class="use-rounded-corner-buttons">${ buttonMarkup } </div>`
53+ }
54+
55+ return buttonMarkup
56+ }
57+
358export default {
459 title : 'Playground/Button' ,
560 argTypes : {
@@ -8,15 +63,18 @@ export default {
863 } ,
964 variant : {
1065 control : 'select' ,
11- options : [ 'default' , 'strong' , 'minimal' , 'negative' ] ,
66+ options : buttonVariants ,
1267 } ,
1368 layout : {
1469 control : 'select' ,
15- options : [ 'Text only' , 'Text + Icon' , 'Icon only' ] ,
70+ options : layouts ,
1671 } ,
1772 state : {
1873 control : 'select' ,
19- options : [ 'enabled' , 'disabled' , 'loading' ] ,
74+ options : states ,
75+ } ,
76+ rounded : {
77+ control : 'boolean' ,
2078 } ,
2179 icon : {
2280 control : 'text' ,
@@ -25,20 +83,41 @@ export default {
2583}
2684
2785export const PlaygroundButton = {
28- render : ( { label, variant, layout, state, icon, ...args } ) => {
29- return `<button class="btn btn-${ variant } ${ layout === 'Icon only' ? ' btn-icon' : '' } ${ state === 'loading' ? ' loading-indeterminate' : '' } "${ state === 'disabled' ? ' disabled' : '' } >
30- ${ layout . includes ( 'Icon' ) ? `<svg ${ layout . includes ( 'Text' ) ? 'aria-hidden="true"' : '' } fill="currentColor" viewBox="0 0 1000 1000">${ icon } </svg>` : '' }
31- ${ ! layout . startsWith ( 'Icon' ) ? label : '' }
32- <svg viewBox='0 0 40 40' xmlns='http://www.w3.org/2000/svg' class="loader" aria-hidden="true">
33- <circle class="loader-inner" cx="20" cy="20" r="17"></circle>
34- </svg>
35- </button>`
86+ parameters : {
87+ docs : {
88+ codePanel : true ,
89+ source : {
90+ transform : ( _src , context ) => {
91+ const { label, variant, layout, state, icon, rounded } = context . args
92+
93+ return renderButton ( {
94+ label,
95+ variant : buttonVariants . includes ( variant ) ? variant : 'default' ,
96+ layout,
97+ state,
98+ icon,
99+ rounded,
100+ } )
101+ } ,
102+ } ,
103+ } ,
104+ } ,
105+ render : ( { label, variant, layout, state, icon, rounded } ) => {
106+ return renderButton ( {
107+ label,
108+ variant : buttonVariants . includes ( variant ) ? variant : 'default' ,
109+ layout,
110+ state,
111+ icon,
112+ rounded,
113+ } )
36114 } ,
37115 args : {
38- label : 'Label 3 ' ,
116+ label : 'Label' ,
39117 variant : 'default' ,
40118 layout : 'Text + Icon' ,
41119 state : 'enabled' ,
120+ rounded : false ,
42121 icon : '<path d="M851.8 172.224C754.746 75.18 597.77 74.613 500 170.492c-97.77-95.879-254.746-95.312-351.795 1.732-96.042 96.038-97.605 250.77-4.7 348.724L500 914v-.006.006l356.5-393.05c92.9-97.956 91.338-252.688-4.7-348.726Zm-65.476 277.332L500 766.694 213.677 449.557l-.145-.162a150.005 150.005 0 0 1 217.526-206.464q.222.222.45.438l43.788 41.64L500 308.784l24.7-23.77 43.794-41.645q.226-.216.449-.438A149.05 149.05 0 0 1 675.018 199a150 150 0 0 1 111.451 250.4Z" style="fill-rule:evenodd"/>'
43122 } ,
44123}
0 commit comments