Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BrandIcon component #60

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions jest.mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const getIconType = (prefix) => {
return 'solid'
case 'fal':
return 'light'
case 'fab':
return 'brand'
default:
throw new Error()
}
Expand Down
23 changes: 23 additions & 0 deletions src/components/BrandIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import React from 'react'

import { IconProp } from '@fortawesome/fontawesome-svg-core'
import { FontAwesomeIcon } from '@fortawesome/react-native-fontawesome'

import BrandIcons, { BrandIconName } from '../lib/BrandIcons'
import theme from '../styles/theme'

type Props = {
name: BrandIconName
color?: string
size?: number
}

const BrandIcon = ({ name, color, size }: Props) => {
const icon = BrandIcons[name]
const iconColor = color ?? theme.color.primary
const iconSize = size ?? theme.icon.size.large

return <FontAwesomeIcon icon={icon as IconProp} color={iconColor} size={iconSize} />
Copy link
Member

@SjaakSchilperoort SjaakSchilperoort Jan 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Die narrowing as IconProp is verdacht, want de types IconDefinition en IconProp hebben geen expliciete overlap. Als je icon op deze manier definieert dan heb je geen as IconProp nodig:

  const { prefix, iconName } = BrandIcons[name]
  const icon = { prefix, iconName } as IconLookup

(disclaimer: heb niet getest of dit nog steeds goed werkt).

EDIT Weet niet of dit een goede oplossing is, want dit zou ook moeten werken const icon: IconLookup = { prefix, iconName } maar geeft nog steeds typing errors.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot to the rescue: het was een versieverschil

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah! Toch goed gevonden 😉

}

export default BrandIcon
25 changes: 25 additions & 0 deletions src/components/__tests__/BrandIcon.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react'

import { render } from '@testing-library/react-native'

import { theme } from '../../styles'
import BrandIcon from '../BrandIcon'

describe('BrandIcon', () => {
describe('Rendering', () => {
test('Normal', () => {
const { toJSON } = render(<BrandIcon name="wikipedia-w" />)
expect(toJSON()).toMatchSnapshot()
})

test('With color', () => {
const { toJSON } = render(<BrandIcon name="wikipedia-w" color={theme.color.error} />)
expect(toJSON()).toMatchSnapshot()
})

test('With size', () => {
const { toJSON } = render(<BrandIcon name="wikipedia-w" size={20} />)
expect(toJSON()).toMatchSnapshot()
})
})
})
28 changes: 28 additions & 0 deletions src/components/__tests__/__snapshots__/BrandIcon.test.tsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`BrandIcon Rendering Normal 1`] = `
<Icon
color="#0066B1"
name="wikipedia-w"
size={16}
type="brand"
/>
`;

exports[`BrandIcon Rendering With color 1`] = `
<Icon
color="#EA554B"
name="wikipedia-w"
size={16}
type="brand"
/>
`;

exports[`BrandIcon Rendering With size 1`] = `
<Icon
color="#0066B1"
name="wikipedia-w"
size={20}
type="brand"
/>
`;
5 changes: 3 additions & 2 deletions src/lib/BrandIcons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { IconName as FontawesomeIconName } from '@fortawesome/fontawesome-svg-co
import { IconDefinition } from '@fortawesome/free-brands-svg-icons'
import { faWikipediaW } from '@fortawesome/free-brands-svg-icons/faWikipediaW'

type IconName = Extract<FontawesomeIconName, 'wikipedia-w'>
type BrandIconName = Extract<FontawesomeIconName, 'wikipedia-w'>

const BrandIcons: { [key in IconName]: IconDefinition } = {
const BrandIcons: { [key in BrandIconName]: IconDefinition } = {
'wikipedia-w': faWikipediaW,
}

export default BrandIcons
export type { BrandIconName }
Loading