gitlab-org--gitlab-foss/app/assets/javascripts/vue_shared/components/icon.vue

73 lines
1.5 KiB
Vue
Raw Normal View History

2017-10-05 17:16:37 -04:00
<script>
2019-07-29 16:25:35 -04:00
import iconsPath from '@gitlab/svgs/dist/icons.svg';
// only allow classes in images.scss e.g. s12
const validSizes = [8, 10, 12, 14, 16, 18, 24, 32, 48, 72];
2018-07-18 12:56:19 -04:00
let iconValidator = () => true;
/*
During development/tests we want to validate that we are just using icons that are actually defined
*/
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line global-require
const data = require('@gitlab/svgs/dist/icons.json');
2018-07-18 12:56:19 -04:00
const { icons } = data;
iconValidator = value => {
if (icons.includes(value)) {
return true;
}
// eslint-disable-next-line no-console
console.warn(`Icon '${value}' is not a known icon of @gitlab/gitlab-svg`);
return false;
};
}
2017-10-05 17:16:37 -04:00
2018-07-18 12:56:19 -04:00
/** This is a re-usable vue component for rendering a svg sprite icon
* @example
* <icon
* name="retry"
* :size="32"
* class="top"
2018-07-18 12:56:19 -04:00
* />
*/
export default {
props: {
name: {
type: String,
required: true,
2018-07-18 12:56:19 -04:00
validator: iconValidator,
},
2017-10-05 17:16:37 -04:00
size: {
type: Number,
required: false,
default: 16,
validator: value => validSizes.includes(value),
},
},
2017-10-05 17:16:37 -04:00
computed: {
spriteHref() {
2019-07-29 16:25:35 -04:00
return `${iconsPath}#${this.name}`;
},
iconTestClass() {
return `ic-${this.name}`;
},
iconSizeClass() {
return this.size ? `s${this.size}` : '';
2017-10-05 17:16:37 -04:00
},
},
};
2017-10-05 17:16:37 -04:00
</script>
2017-10-05 17:16:37 -04:00
<template>
<svg
:key="spriteHref"
:class="[iconSizeClass, iconTestClass]"
aria-hidden="true"
v-on="$listeners"
>
2018-11-16 15:07:38 -05:00
<use v-bind="{ 'xlink:href': spriteHref }" />
2017-10-05 17:16:37 -04:00
</svg>
</template>