Skip to content

Re-enable ability to override icons in "Topics" section #939

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/components/DocumentationTopic/TopicLinkBlockIcon.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
-->

<template>
<div class="topic-icon-wrapper" v-if="icon">
<component :is="icon" class="topic-icon" />
<div class="topic-icon-wrapper" v-if="imageOverride || icon">
<OverridableAsset
v-if="imageOverride"
:imageOverride="imageOverride"
class="topic-icon icon-override" />
<component v-else-if="icon" :is="icon" class="topic-icon" />
</div>
</template>

Expand All @@ -24,6 +28,7 @@ import TechnologyIcon from 'theme/components/Icons/TechnologyIcon.vue';
import TutorialIcon from 'theme/components/Icons/TutorialIcon.vue';
import SVGIcon from 'docc-render/components/SVGIcon.vue';
import { TopicRole } from 'docc-render/constants/roles';
import OverridableAsset from 'docc-render/components/OverridableAsset.vue';

const TopicRoleIcons = {
[TopicRole.article]: ArticleIcon,
Expand All @@ -39,12 +44,16 @@ const TopicRoleIcons = {
};

export default {
components: { SVGIcon },
components: { OverridableAsset, SVGIcon },
props: {
role: {
type: String,
required: true,
},
imageOverride: {
type: Object,
default: null,
},
},

computed: {
Expand Down Expand Up @@ -84,4 +93,12 @@ export default {
height: rem(17px);
}
}

// Since we are unable to enforce the actual SVG color when it is embedded in
// an <img> element, this workaround will force a gray color by applying a
// grayscale filter to ensure the color of the icon is consistent with the
// normal, builtin icons
.icon-override:deep(img) {
filter: grayscale(1);
}
</style>
1 change: 1 addition & 0 deletions src/components/DocumentationTopic/TopicsLinkBlock.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<TopicLinkBlockIcon
v-if="topic.role && !change"
:role="topic.role"
:imageOverride="references[iconOverride]"
/>
<DecoratedTopicTitle v-if="topic.fragments" :tokens="topic.fragments" />
<WordBreak v-else :tag="titleTag">{{ topic.title }}</WordBreak>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { mount } from '@vue/test-utils';
import { TopicRole } from '@/constants/roles';
import ArticleIcon from '@/components/Icons/ArticleIcon.vue';
import TechnologyIcon from '@/components/Icons/TechnologyIcon.vue';
import OverridableAsset from '@/components/OverridableAsset.vue';

const defaultProps = {
role: TopicRole.article,
Expand All @@ -32,9 +33,31 @@ describe('TopicLinkBlockIcon', () => {
expect(wrapper.find('.topic-icon').is(ArticleIcon)).toBe(true);
});

it('renders nothing if no role', () => {
it('renders an override icon from an image override', () => {
const imageOverride = {
variants: [{
url: '/foo/bar',
svgID: 'foo',
}],
};
const wrapper = createWrapper({
propsData: {
imageOverride,
},
});
const icon = wrapper.find('.topic-icon');
expect(icon.classes('icon-override')).toBe(true);
expect(icon.is(ArticleIcon)).toBe(false);
expect(icon.is(OverridableAsset)).toBe(true);
expect(icon.props()).toMatchObject({
imageOverride,
});
});

it('renders nothing if no role or image override', () => {
const wrapper = createWrapper({
propsData: {
imageOverride: null,
role: TopicRole.devLink, // no icon for this
},
});
Expand Down
23 changes: 22 additions & 1 deletion tests/unit/components/DocumentationTopic/TopicsLinkBlock.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,22 @@ describe('TopicsLinkBlock', () => {
/** @type {import('@vue/test-utils').Wrapper} */
let wrapper;

const iconOverride = {
type: 'icon',
identifier: 'icon-override',
};

const references = {
[iconOverride.identifier]: { foo: 'bar' },
};

const store = {
reset: jest.fn(),
setAPIChanges: jest.fn(),
state: {
onThisPageSections: [],
apiChanges: null,
references: {},
references,
},
};

Expand Down Expand Up @@ -126,6 +135,18 @@ describe('TopicsLinkBlock', () => {
expect(link.props('role')).toBe(propsData.topic.role);
});

it('renders a TopicLinkBlockIcon with an override', () => {
const icon = wrapper.find(TopicLinkBlockIcon);
expect(icon.props('imageOverride')).toBe(null);
wrapper.setProps({
topic: {
...propsData.topic,
images: [iconOverride, { type: 'card', identifier: 'foo' }],
},
});
expect(icon.props('imageOverride')).toBe(references[iconOverride.identifier]);
});

it('renders a normal `WordBreak` for the link text', () => {
const wordBreak = wrapper.find('.link').find(WordBreak);
expect(wordBreak.exists()).toBe(true);
Expand Down