-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from josemarluedke/feat/core-close-button
[core] Create CloseButton component
- Loading branch information
Showing
11 changed files
with
299 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<button | ||
{{on "click" this.handleClick}} | ||
type="button" | ||
class="close-button {{this.sizeClass}}" | ||
...attributes | ||
> | ||
{{#let (concat "close-button__icon " this.sizeClass "__icon") as |iconClassName|}} | ||
{{#if hasBlock}} | ||
{{yield iconClassName}} | ||
{{else}} | ||
<svg | ||
class={{iconClassName}} | ||
aria-hidden="true" | ||
xmlns="http://www.w3.org/2000/svg" | ||
fill="none" | ||
viewBox="0 0 24 24" | ||
stroke="currentColor" | ||
> | ||
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" /> | ||
</svg> | ||
{{/if}} | ||
{{/let}} | ||
|
||
<VisuallyHidden> | ||
{{if @title @title "Close"}} | ||
</VisuallyHidden> | ||
</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Component from '@glimmer/component'; | ||
import { action } from '@ember/object'; | ||
|
||
interface CloseButtonArgs { | ||
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl'; | ||
onClick?: (event: Event) => void; | ||
} | ||
|
||
export default class CloseButton extends Component<CloseButtonArgs> { | ||
@action handleClick(event: Event): void { | ||
this.args.onClick?.(event); | ||
} | ||
|
||
get sizeClass(): string { | ||
return `close-button--${this.args.size || 'sm'}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from '@frontile/core/components/close-button'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
const defaultTheme = require('tailwindcss/resolveConfig')( | ||
require('tailwindcss/defaultConfig') | ||
).theme; | ||
|
||
const defaultConfig = { | ||
closeButton: { | ||
backgroundColor: undefined, | ||
hoverBackgroundColor: defaultTheme.colors.gray[100], | ||
borderRadius: defaultTheme.borderRadius.full, | ||
color: 'inherit', | ||
|
||
sizes: { | ||
xs: defaultTheme.fontSize.sm, | ||
sm: defaultTheme.fontSize.base, | ||
md: defaultTheme.fontSize.xl, | ||
lg: defaultTheme.fontSize['2xl'], | ||
xl: defaultTheme.fontSize['4xl'] | ||
} | ||
} | ||
}; | ||
|
||
function defaultOptions({ config }) { | ||
return { | ||
closeButton: { | ||
baseStyle: { | ||
backgroundColor: config.closeButton.backgroundColor, | ||
borderRadius: config.closeButton.borderRadius, | ||
transitionProperty: defaultTheme.transitionProperty.default, | ||
transitionDuration: defaultTheme.transitionDuration[200], | ||
|
||
'&.focus-visible:focus': { | ||
outline: 'none', | ||
boxShadow: defaultTheme.boxShadow.outline | ||
}, | ||
|
||
'&:hover': { | ||
backgroundColor: config.closeButton.hoverBackgroundColor | ||
} | ||
}, | ||
variants: { | ||
xs: { | ||
baseStyle: { | ||
fontSize: config.closeButton.sizes.xs, | ||
padding: defaultTheme.spacing[2] | ||
} | ||
}, | ||
sm: { | ||
baseStyle: { | ||
fontSize: config.closeButton.sizes.sm, | ||
padding: defaultTheme.spacing[1] | ||
} | ||
}, | ||
md: { | ||
baseStyle: { | ||
fontSize: config.closeButton.sizes.md, | ||
padding: defaultTheme.spacing[2] | ||
} | ||
}, | ||
lg: { | ||
baseStyle: { | ||
fontSize: config.closeButton.sizes.lg, | ||
padding: defaultTheme.spacing[3] | ||
} | ||
}, | ||
xl: { | ||
baseStyle: { | ||
fontSize: config.closeButton.sizes.xl, | ||
padding: defaultTheme.spacing[3] | ||
} | ||
} | ||
}, | ||
|
||
parts: { | ||
icon: { | ||
height: '1em', | ||
width: '1em' | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
module.exports = { | ||
defaultConfig, | ||
defaultOptions | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const plugin = require('tailwindcss/plugin'); | ||
const { | ||
resolve, | ||
isEmpty, | ||
kebabCase | ||
} = require('@frontile/tailwindcss-plugin-helpers'); | ||
|
||
module.exports = plugin.withOptions(function (userConfig) { | ||
return function ({ addComponents, theme }) { | ||
const { options } = resolve( | ||
'@frontile/core', | ||
require('./default-options'), | ||
userConfig, | ||
theme | ||
); | ||
|
||
function addStylesFor(base, options, modifier = '') { | ||
if (isEmpty(options)) { | ||
return; | ||
} | ||
|
||
if (modifier !== '') { | ||
base += `--${modifier}`; | ||
} | ||
|
||
const { baseStyle, variants, parts } = options; | ||
|
||
addComponents({ | ||
[base]: baseStyle | ||
}); | ||
|
||
if (!isEmpty(parts)) { | ||
Object.keys(parts).forEach((key) => { | ||
addComponents({ | ||
[`${base}__${kebabCase(key)}`]: parts[key] | ||
}); | ||
}); | ||
} | ||
|
||
if (!isEmpty(variants)) { | ||
Object.keys(variants).forEach((key) => { | ||
addStylesFor(base, variants[key], kebabCase(key)); | ||
}); | ||
} | ||
} | ||
|
||
addStylesFor('.close-button', options.closeButton); | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
@tailwind base; | ||
|
||
@tailwind components; | ||
|
||
@tailwind utilities; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* eslint-disable node/no-extraneous-require */ | ||
|
||
module.exports = { | ||
future: { | ||
removeDeprecatedGapUtilities: true, | ||
purgeLayersByDefault: true | ||
}, | ||
purge: [], | ||
theme: { | ||
extend: {} | ||
}, | ||
variants: {}, | ||
plugins: [require('@frontile/core/tailwind')] | ||
}; |
74 changes: 74 additions & 0 deletions
74
packages/core/tests/integration/components/close-button-test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { render } from '@ember/test-helpers'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
|
||
module('Integration | Component | CloseButton', function (hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
test('it renders title', async function (assert) { | ||
await render(hbs`<CloseButton @title={{this.title}} />`); | ||
|
||
assert.equal(this.element.textContent?.trim(), 'Close'); | ||
|
||
this.set('title', 'Fechar Menu'); | ||
assert.equal(this.element.textContent?.trim(), 'Fechar Menu'); | ||
}); | ||
|
||
test('it allows to yield', async function (assert) { | ||
await render( | ||
hbs`<CloseButton><div class="icon">Here is my icon</div></CloseButton>` | ||
); | ||
assert.dom('.close-button .icon').exists(); | ||
|
||
assert.dom('.close-button .icon').hasText('Here is my icon'); | ||
}); | ||
|
||
test('it yields the icon classes', async function (assert) { | ||
await render( | ||
hbs`<CloseButton as |class|><div class="icon">{{class}}</div></CloseButton>` | ||
); | ||
assert | ||
.dom('.close-button .icon') | ||
.hasText('close-button__icon close-button--sm__icon'); | ||
}); | ||
|
||
test('it adds size classes', async function (assert) { | ||
await render(hbs`<CloseButton @size={{this.size}} />`); | ||
|
||
assert.dom('.close-button').hasClass('close-button--sm'); | ||
assert | ||
.dom('.close-button .close-button__icon') | ||
.hasClass('close-button--sm__icon'); | ||
|
||
this.set('size', 'xm'); | ||
assert.dom('.close-button').hasClass('close-button--xm'); | ||
assert | ||
.dom('.close-button .close-button__icon') | ||
.hasClass('close-button--xm__icon'); | ||
|
||
this.set('size', 'sm'); | ||
assert.dom('.close-button').hasClass('close-button--sm'); | ||
assert | ||
.dom('.close-button .close-button__icon') | ||
.hasClass('close-button--sm__icon'); | ||
|
||
this.set('size', 'md'); | ||
assert.dom('.close-button').hasClass('close-button--md'); | ||
assert | ||
.dom('.close-button .close-button__icon') | ||
.hasClass('close-button--md__icon'); | ||
|
||
this.set('size', 'lg'); | ||
assert.dom('.close-button').hasClass('close-button--lg'); | ||
assert | ||
.dom('.close-button .close-button__icon') | ||
.hasClass('close-button--lg__icon'); | ||
|
||
this.set('size', 'xl'); | ||
assert.dom('.close-button').hasClass('close-button--xl'); | ||
assert | ||
.dom('.close-button .close-button__icon') | ||
.hasClass('close-button--xl__icon'); | ||
}); | ||
}); |