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

feat(NcIconSvgWrapper): allow to render raw svg paths #4643

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"@babel/preset-typescript": "^7.22.5",
"@cypress/vue2": "^2.0.1",
"@fontsource/roboto": "^5.0.0",
"@mdi/js": "^7.3.67",
"@mdi/svg": "^7.0.96",
"@nextcloud/babel-config": "^1.0.0",
"@nextcloud/browserslist-config": "^3.0.0",
Expand Down
42 changes: 31 additions & 11 deletions src/components/NcIconSvgWrapper/NcIconSvgWrapper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ Render raw SVG string icons.
</NcButton>
<NcButton aria-label="Send">
<template #icon>
<NcIconSvgWrapper :svg="sendSvg" name="Send" />
<NcIconSvgWrapper :path="mdiSend" name="Send" />
</template>
</NcButton>
<NcButton aria-label="Star">
<template #icon>
<NcIconSvgWrapper :svg="starSvg" name="Star" />
<NcIconSvgWrapper :path="mdiStar" name="Star" />
</template>
</NcButton>
</div>
Expand All @@ -62,17 +62,17 @@ Render raw SVG string icons.
import closeSvg from '@mdi/svg/svg/close.svg?raw'
import cogSvg from '@mdi/svg/svg/cog.svg?raw'
import plusSvg from '@mdi/svg/svg/plus.svg?raw'
import sendSvg from '@mdi/svg/svg/send.svg?raw'
import starSvg from '@mdi/svg/svg/star.svg?raw'
import { mdiSend } from '@mdi/js'
import { mdiStar } from '@mdi/js'

export default {
data() {
return {
closeSvg,
cogSvg,
plusSvg,
sendSvg,
starSvg,
mdiSend,
mdiStar,
}
},
}
Expand All @@ -89,10 +89,14 @@ export default {
</docs>

<template>
<span class="icon-vue"
role="img"
:aria-hidden="!name ? true : undefined"
:aria-label="name || undefined"
<span v-if="!cleanSvg"
v-bind="attributes">
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
<path :d="path" />
</svg>
</span>
<span v-else
v-bind="attributes"
v-html="cleanSvg" /> <!-- eslint-disable-line vue/no-v-html -->
</template>

Expand All @@ -119,11 +123,19 @@ export default {
type: String,
default: '',
},

/**
* Raw SVG path to render. Takes precedence over the SVG string in the `svg` prop.
*/
path: {
type: String,
default: '',
},
},

computed: {
cleanSvg() {
if (!this.svg) {
if (!this.svg || this.path) {
return
}

Expand All @@ -142,6 +154,14 @@ export default {

return svgDocument.documentElement.outerHTML
},
attributes() {
return {
class: 'icon-vue',
role: 'img',
'aria-hidden': !this.name ? true : undefined,
'aria-label': this.name || undefined,
}
},
},
}
</script>
Expand Down