import { CdxIcon, CdxButton } from '@wikimedia/codex'; import '../node_modules/@wikimedia/codex/dist/codex.style.css'; import { h, createApp } from 'vue'; import buttonTemplate from '!!raw-loader!../includes/templates/Button.mustache'; import mustache from 'mustache'; import { cdxIconAdd } from '@wikimedia/codex-icons'; export default { title: 'Icon and Buttons' }; /** * * @typedef {Object} ButtonProps * @property {string} weight * @property {string} action */ /** * @param {ButtonProps} props * @param {string} label * @return {string} */ function makeButtonLegacy( props, label ) { let typeClass = ''; let iconClass = 'mw-ui-icon-add'; switch ( props.action ) { case 'progressive': typeClass += ' mw-ui-progressive'; iconClass += '-progressive'; break; case 'destructive': typeClass += ' mw-ui-destructive'; iconClass += '-destructive'; break; } if ( props.weight === 'primary' ) { iconClass = 'mw-ui-icon-add-invert'; } return mustache.render( buttonTemplate, { label, class: typeClass, 'is-quiet': props.weight === 'quiet', 'html-vector-button-icon': `` } ); } /** * @param {ButtonProps} props * @param {string} text * @param {string} icon * @return {string} */ function makeButton( props, text, icon ) { const el = document.createElement( 'div' ); const vm = createApp( { render: function () { // @ts-ignore return h( CdxButton, props, [ h( CdxIcon, { icon } ), text ] ); } } ); vm.mount( el ); return ` ${makeButtonLegacy( props, text )} ${el.outerHTML} `; } /** * @return {string} */ function makeIcon() { const el = document.createElement( 'div' ); const vm = createApp( { render: function () { // @ts-ignore return h( CdxButton, { 'aria-label': 'add' }, [ h( CdxIcon, { icon: cdxIconAdd } ) ] ); } } ); vm.mount( el ); const elQuiet = document.createElement( 'div' ); const vmQuiet = createApp( { render: function () { // @ts-ignore return h( CdxButton, { weight: 'quiet', 'aria-label': 'add' }, [ h( CdxIcon, { icon: cdxIconAdd } ) ] ); } } ); vmQuiet.mount( elQuiet ); return `
Regular icon
${ mustache.render( buttonTemplate, { label: 'Normal Icon button', icon: 'add' } ) }
Regular icon
${el.outerHTML}
Small icon button
${ mustache.render( buttonTemplate, { label: 'Small icon button', class: 'mw-ui-icon-small', icon: 'add' } ) }
Small icon button
${el.outerHTML}
Quiet icon button
${ mustache.render( buttonTemplate, { label: 'Quiet Icon', 'is-quiet': true, icon: 'add' } ) }
Quiet icon button
${elQuiet.outerHTML} `; } /** * * @param {string[]} btns * @return {string} */ function makeButtons( btns ) { return ` ${btns.join( '\n' )}
Legacy Codex
`; } /** * @return {string} */ export const Button = () => makeButtons( [ makeButton( { action: 'default', weight: 'quiet' }, 'Quiet button', cdxIconAdd ), makeButton( { action: 'progressive', weight: 'quiet' }, 'Quiet progressive', cdxIconAdd ), makeButton( { action: 'destructive', weight: 'quiet' }, 'Quiet destructive', cdxIconAdd ), makeButton( { action: 'default', weight: 'normal' }, 'Normal', cdxIconAdd ), makeButton( { weight: 'primary', action: 'progressive' }, 'Progressive primary', cdxIconAdd ), makeButton( { weight: 'primary', action: 'destructive' }, 'Destructive primary', cdxIconAdd ), makeIcon() ] );