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

112 lines
2.1 KiB
Vue
Raw Normal View History

2018-03-17 18:26:18 +05:30
<script>
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"
* css-classes="top"
* />
*/
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,
validator(value) {
return validSizes.includes(value);
2018-03-17 18:26:18 +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
cssClasses: {
type: String,
required: false,
default: '',
},
2018-03-17 18:26:18 +05:30
2018-10-15 14:42:47 +05:30
width: {
type: Number,
required: false,
default: null,
},
2018-03-17 18:26:18 +05:30
2018-10-15 14:42:47 +05:30
height: {
type: Number,
required: false,
default: null,
},
2018-03-17 18:26:18 +05:30
2018-10-15 14:42:47 +05:30
y: {
type: Number,
required: false,
default: null,
},
2018-03-17 18:26:18 +05:30
2018-10-15 14:42:47 +05:30
x: {
type: Number,
required: false,
default: null,
2018-03-17 18:26:18 +05:30
},
2018-11-18 11:00:15 +05:30
tabIndex: {
type: String,
required: false,
default: null,
},
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() {
return `${gon.sprite_icons}#${this.name}`;
},
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>
<svg
2018-10-15 14:42:47 +05:30
:class="[iconSizeClass, iconTestClass, cssClasses]"
2018-03-17 18:26:18 +05:30
:width="width"
:height="height"
:x="x"
2018-10-15 14:42:47 +05:30
:y="y"
2018-11-18 11:00:15 +05:30
:tabindex="tabIndex"
2018-12-13 13:39:08 +05:30
aria-hidden="true"
2018-10-15 14:42:47 +05:30
>
2019-02-15 15:39:39 +05:30
<use v-bind="{ 'xlink:href': spriteHref }" />
2018-03-17 18:26:18 +05:30
</svg>
</template>