debian-mirror-gitlab/app/assets/javascripts/vue_shared/components/icon.vue

68 lines
1.5 KiB
Vue
Raw Normal View History

2018-03-17 18:26:18 +05:30
<script>
2019-10-12 21:52:04 +05:30
import iconsPath from '@gitlab/svgs/dist/icons.svg';
2018-11-18 11:00:15 +05:30
// only allow classes in images.scss e.g. s12
2019-02-15 15:39:39 +05:30
const validSizes = [8, 10, 12, 14, 16, 18, 24, 32, 48, 72];
2018-11-18 11:00:15 +05:30
let iconValidator = () => true;
2018-03-17 18:26:18 +05:30
2018-11-18 11:00:15 +05:30
/*
During development/tests we want to validate that we are just using icons that are actually defined
2018-10-15 14:42:47 +05:30
*/
2018-11-18 11:00:15 +05:30
if (process.env.NODE_ENV !== 'production') {
// eslint-disable-next-line global-require
2018-12-13 13:39:08 +05:30
const data = require('@gitlab/svgs/dist/icons.json');
2018-11-18 11:00:15 +05:30
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;
};
}
2018-03-17 18:26:18 +05:30
2018-11-18 11:00:15 +05:30
/** This is a re-usable vue component for rendering a svg sprite icon
* @example
* <icon
* name="retry"
* :size="32"
2019-12-21 20:55:43 +05:30
* class="top"
2018-11-18 11:00:15 +05:30
* />
*/
2018-10-15 14:42:47 +05:30
export default {
props: {
name: {
type: String,
required: true,
2018-11-18 11:00:15 +05:30
validator: iconValidator,
2018-10-15 14:42:47 +05:30
},
2018-03-17 18:26:18 +05:30
2018-10-15 14:42:47 +05:30
size: {
type: Number,
required: false,
default: 16,
2019-12-21 20:55:43 +05:30
validator: value => validSizes.includes(value),
2018-11-18 11:00:15 +05:30
},
2018-10-15 14:42:47 +05:30
},
2018-03-17 18:26:18 +05:30
2018-10-15 14:42:47 +05:30
computed: {
spriteHref() {
2019-10-12 21:52:04 +05:30
return `${iconsPath}#${this.name}`;
2018-10-15 14:42:47 +05:30
},
iconTestClass() {
return `ic-${this.name}`;
},
iconSizeClass() {
return this.size ? `s${this.size}` : '';
2018-03-17 18:26:18 +05:30
},
2018-10-15 14:42:47 +05:30
},
};
2018-03-17 18:26:18 +05:30
</script>
<template>
2019-12-26 22:10:19 +05:30
<svg :class="[iconSizeClass, iconTestClass]" aria-hidden="true" v-on="$listeners">
2019-02-15 15:39:39 +05:30
<use v-bind="{ 'xlink:href': spriteHref }" />
2018-03-17 18:26:18 +05:30
</svg>
</template>