Skip to content

Commit d00c774

Browse files
feat: add font size prop into icon component (#379)
1 parent f7b39f4 commit d00c774

File tree

5 files changed

+51
-22
lines changed

5 files changed

+51
-22
lines changed

packages/core/src/components.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export namespace Components {
9595
interface AtomIcon {
9696
"color"?: Color;
9797
"icon"?: IconProps;
98-
"size"?: 'small' | 'large';
98+
"size"?: Size;
9999
}
100100
interface AtomInput {
101101
"autocomplete"?: 'on' | 'off';
@@ -543,7 +543,7 @@ declare namespace LocalJSX {
543543
interface AtomIcon {
544544
"color"?: Color;
545545
"icon"?: IconProps;
546-
"size"?: 'small' | 'large';
546+
"size"?: Size;
547547
}
548548
interface AtomInput {
549549
"autocomplete"?: 'on' | 'off';
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,32 @@
1-
import { newSpecPage } from '@stencil/core/testing'
1+
import { SpecPage, newSpecPage } from '@stencil/core/testing'
22

33
import { AtomIcon } from './icon'
44

55
const URL_MOCK = 'https://atomium.juntossomosmais.com.br/icons'
66

77
describe('atom-icon', () => {
8-
it('should render ion-icon element', async () => {
9-
const page = await newSpecPage({
8+
let page: SpecPage
9+
10+
beforeEach(async () => {
11+
page = await newSpecPage({
1012
components: [AtomIcon],
1113
html: `<atom-icon icon="heart" color="primary" size="large"></atom-icon>`,
1214
})
15+
})
16+
17+
it('should size attributte to be filed with size prop when its an enum type', () => {
18+
expect(page?.root?.shadowRoot).toEqualHtml(`
19+
<ion-icon color="primary" icon="${URL_MOCK}/heart.svg" size="large"></ion-icon>
20+
`)
21+
})
1322

14-
if (!page?.root?.shadowRoot) {
15-
throw new Error('page.root is undefined')
16-
}
23+
it('should style font-size to be filed with size prop when its an number type', async () => {
24+
page?.root?.setAttribute('size', '10')
1725

1826
await page.waitForChanges()
1927

20-
expect(page.root).toEqualHtml(`
21-
<atom-icon aria-hidden="true" color="primary" icon="heart" size="large">
22-
<mock:shadow-root>
23-
<ion-icon color="primary" icon="${URL_MOCK}/heart.svg" size="large"></ion-icon>
24-
</mock:shadow-root>
25-
</atom-icon>
28+
expect(page?.root?.shadowRoot).toEqualHtml(`
29+
<ion-icon color="primary" icon="${URL_MOCK}/heart.svg" size="" style="font-size: 10px;"></ion-icon>
2630
`)
2731
})
2832
})

packages/core/src/components/icon/icon.tsx

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Color } from '@ionic/core'
2-
import { Component, Host, Prop, h } from '@stencil/core'
2+
import { Component, Host, Prop, State, Watch, h } from '@stencil/core'
33

44
import { IconProps } from '../../icons'
55

66
const CDN_URL = 'https://atomium.juntossomosmais.com.br/icons'
77

8+
type Size = 'small' | 'large' | number
9+
810
@Component({
911
tag: 'atom-icon',
1012
styleUrl: 'icon.scss',
@@ -13,15 +15,39 @@ const CDN_URL = 'https://atomium.juntossomosmais.com.br/icons'
1315
export class AtomIcon {
1416
@Prop() color?: Color
1517
@Prop() icon?: IconProps
16-
@Prop() size?: 'small' | 'large'
18+
@Prop() size?: Size
19+
20+
@State() validateSize?: string = ''
21+
@State() fontSize?: string = ''
22+
23+
private updateSize(newSize: Size) {
24+
this.validateSize = ''
25+
this.fontSize = ''
26+
27+
if (newSize === 'small' || newSize === 'large') {
28+
this.validateSize = newSize
29+
} else if (newSize > 0) {
30+
this.fontSize = `${newSize}px`
31+
}
32+
}
33+
34+
@Watch('size')
35+
watchPropHandler(newSize: Size) {
36+
this.updateSize(newSize)
37+
}
38+
39+
componentWillLoad() {
40+
this.updateSize(this.size)
41+
}
1742

1843
render(): JSX.Element {
1944
return (
2045
<Host aria-hidden='true'>
2146
<ion-icon
2247
icon={`${CDN_URL}/${this.icon}.svg`}
2348
color={this.color}
24-
size={this.size}
49+
size={this.validateSize}
50+
style={{ fontSize: this.fontSize }}
2551
/>
2652
</Host>
2753
)

packages/core/src/components/icon/stories/icon.args.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ export const IconStoryArgs = {
3636
},
3737
},
3838
size: {
39-
control: 'select',
40-
description: 'The size of the icon.',
41-
options: ['small', 'large'],
39+
control: 'text',
40+
description:
41+
'The size of the icon. Use large or small to change the size of the icon or pass a number to set the font-size in pixels.',
4242
table: {
4343
category: Category.PROPERTIES,
4444
},
@@ -49,5 +49,5 @@ export const IconStoryArgs = {
4949
export const IconComponentArgs = {
5050
icon: 'heart',
5151
color: 'secondary',
52-
size: 'large',
52+
size: '',
5353
}

packages/core/src/components/icon/stories/icon.core.stories.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Meta, StoryObj } from '@storybook/web-components'
2-
32
import { html } from 'lit'
43

54
import { IconComponentArgs, IconStoryArgs } from './icon.args'

0 commit comments

Comments
 (0)