Skip to content

Commit

Permalink
chore: add ability to resize SVG icons with width and height props
Browse files Browse the repository at this point in the history
  • Loading branch information
bennyxguo committed Aug 6, 2023
1 parent b5cfe0c commit c6fb706
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 23 deletions.
16 changes: 12 additions & 4 deletions src/components/Header/src/Controls.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@
data-dia="search"
@click="handleOpenModal(true)"
>
<SvgIcon icon-class="search" stroke="var(--text-white)" />
<SvgIcon
icon-class="search"
stroke="var(--text-white)"
width="2rem"
height="2rem"
/>
</span>
<Dropdown v-if="enableMultiLanguage" @command="handleClick">
<span class="ob-drop-shadow" data-dia="language">
<SvgIcon icon-class="globe" stroke="var(--text-white)" />
<SvgIcon
icon-class="globe"
stroke="var(--text-white)"
width="2rem"
height="2rem"
/>
<span v-if="$i18n.locale == 'cn'">中文</span>
<span v-if="$i18n.locale == 'en'">EN</span>
</span>
Expand Down Expand Up @@ -93,8 +103,6 @@ export default defineComponent({
}
.svg-icon {
stroke: #fff;
height: 2rem;
width: 2rem;
margin-right: 0.5rem;
pointer-events: none;
}
Expand Down
51 changes: 40 additions & 11 deletions src/components/SvgIcon/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
class="svg-external-icon svg-icon"
v-bind="$attrs"
/>
<svg v-else :class="svgClass" aria-hidden="true" v-bind="$attrs">
<svg
v-else
:class="svgClass"
aria-hidden="true"
v-bind="$attrs"
:style="{ height: svgStyle.height, width: svgStyle.width }"
>
<use
:href="iconName"
:fill="fill !== '' ? fill : svgStyle.fill"
Expand Down Expand Up @@ -47,9 +53,25 @@ export default defineComponent({
svgType: {
type: String as PropType<SvgTypes>,
default: 'fill'
},
width: {
type: String,
default: '1em'
},
height: {
type: String,
default: '1em'
}
},
setup(props: { iconClass: string; className: string; svgType: SvgTypes }) {
setup(props: {
iconClass: string
className: string
svgType: SvgTypes
fill: string
stroke: string
width: string
height: string
}) {
const appStore = useAppStore()
const isExternalClass = computed(() => isExternalIcon(props.iconClass))
Expand All @@ -74,16 +96,25 @@ export default defineComponent({
svgStyle: computed(() => {
if (props.svgType === SvgTypes.fill) {
return {
fill: 'currentColor',
stroke:
appStore.theme === 'theme-dark'
? 'var(--background-primary)'
: 'white'
fill: props.fill ? props.fill : 'currentColor',
stroke: props.stroke
? props.stroke
: appStore.theme === 'theme-dark'
? 'var(--background-primary)'
: 'white',
width: props.width,
height: props.height
}
} else {
return {
fill: 'none',
stroke: appStore.theme === 'theme-dark' ? 'white' : 'currentColor'
fill: props.fill ? props.fill : 'none',
stroke: props.stroke
? props.stroke
: appStore.theme === 'theme-dark'
? 'white'
: 'currentColor',
width: props.width,
height: props.height
}
}
}),
Expand All @@ -98,8 +129,6 @@ export default defineComponent({

<style scoped>
.svg-icon {
width: 1em;
height: 1em;
vertical-align: -0.15em;
overflow: hidden;
display: inline;
Expand Down
16 changes: 10 additions & 6 deletions src/icons/clock-outline.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions src/icons/text-outline.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c6fb706

Please sign in to comment.