Skip to content

Commit

Permalink
feat(smart-snippet): allow link _target modifs (#3026)
Browse files Browse the repository at this point in the history
* feat(smart-snippet): allow link _target modifs

* test(smart-snippet): add slot cypress test

* test(smart-snippet): fix cypress test

* Update packages/atomic/src/pages/examples/ipx.html

Co-authored-by: Benjamin Taillon <[email protected]>

* style(smart-snippets): name refactor + jsdoc

* test(smart-snippets): update cypress tests

---------

Co-authored-by: Benjamin Taillon <[email protected]>
  • Loading branch information
dguerinCoveo and btaillon-coveo authored Jul 26, 2023
1 parent c928679 commit 8a9342a
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/atomic/cypress/e2e/smart-snippet-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export const addSmartSnippetDefaultOptions: Required<AddSmartSnippetOptions> = {
};

export const addSmartSnippet =
(options: AddSmartSnippetOptions = {}) =>
(options: AddSmartSnippetOptions = {}, slot?: HTMLElement) =>
(fixture: TestFixture) => {
const element = generateComponentHTML(
'atomic-smart-snippet',
Expand All @@ -80,6 +80,9 @@ export const addSmartSnippet =
element.append(
...toArray(options.content ?? addSmartSnippetDefaultOptions.content)
);
if (slot) {
element.append(slot);
}
fixture
.withStyle(
`html { font-size: ${
Expand Down
13 changes: 13 additions & 0 deletions packages/atomic/cypress/e2e/smart-snippet.cypress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -525,4 +525,17 @@ describe('Smart Snippet Test Suites', () => {
.should('equal', 'rgb(84, 170, 255)');
});
});

describe('when there is a valid slot named "source-anchor-attributes"', () => {
beforeEach(() => {
const slot = generateComponentHTML('a', {
target: '_blank',
slot: 'source-anchor-attributes',
});
new TestFixture().with(addSmartSnippet({}, slot)).init();
});
it('copies the attributes properly', () => {
SmartSnippetSelectors.sourceUrl().should('have.attr', 'target', '_blank');
});
});
});
2 changes: 2 additions & 0 deletions packages/atomic/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1857,6 +1857,7 @@ export namespace Components {
"source"?: HTMLElement;
}
interface AtomicSmartSnippetSource {
"anchorAttributes"?: Attr[];
"source": Result;
}
interface AtomicSmartSnippetSuggestions {
Expand Down Expand Up @@ -4745,6 +4746,7 @@ declare namespace LocalJSX {
"source"?: HTMLElement;
}
interface AtomicSmartSnippetSource {
"anchorAttributes"?: Attr[];
"onBeginDelayedSelectSource"?: (event: AtomicSmartSnippetSourceCustomEvent<any>) => void;
"onCancelPendingSelectSource"?: (event: AtomicSmartSnippetSourceCustomEvent<any>) => void;
"onSelectSource"?: (event: AtomicSmartSnippetSourceCustomEvent<any>) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class AtomicSmartSnippetSource
{
@InitializeBindings() public bindings!: AnyBindings;
@Prop({reflect: true, mutable: true}) source!: Result;
@Prop() anchorAttributes?: Attr[];

@Event() selectSource!: EventEmitter;
@Event() beginDelayedSelectSource!: EventEmitter;
Expand All @@ -55,6 +56,7 @@ export class AtomicSmartSnippetSource
href={this.source.clickUri}
className="block"
part="source-url"
attributes={this.anchorAttributes}
onSelect={() => this.selectSource.emit()}
onBeginDelayedSelect={() => this.beginDelayedSelectSource.emit()}
onCancelPendingSelect={() => this.cancelPendingSelectSource.emit()}
Expand All @@ -64,6 +66,7 @@ export class AtomicSmartSnippetSource
<LinkWithResultAnalytics
title={this.source.title}
href={this.source.clickUri}
attributes={this.anchorAttributes}
className="block"
part="source-title"
onSelect={() => this.selectSource.emit()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {AnyBindings} from '../../interface/bindings';

interface SmartSnippetSuggestionProps {
id: string;
getSourceAnchorAttributes?: () => Attr[] | undefined;
getHost: () => HTMLElement;
getBindings: () => AnyBindings;
getHeadingLevel: () => number;
Expand Down Expand Up @@ -128,6 +129,7 @@ export class SmartSnippetSuggestionCommon {
{
<atomic-smart-snippet-source
source={source}
anchorAttributes={this.props.getSourceAnchorAttributes?.()}
onSelectSource={() =>
this.props
.getQuestionsList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type FeedBackModalElement =
interface SmartSnippetProps {
id: string;
modalTagName: string;
getSourceAnchorAttributes?: () => Attr[] | undefined;
getHost: () => HTMLElement;
getBindings: () => AnyBindings;
getModalRef: () => FeedBackModalElement | undefined;
Expand Down Expand Up @@ -152,6 +153,7 @@ export class SmartSnippetCommon {
>
{source && (
<atomic-smart-snippet-source
anchorAttributes={this.props.getSourceAnchorAttributes?.()}
source={source}
onSelectSource={this.props.getSmartSnippet().selectSource}
onBeginDelayedSelectSource={
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
BindStateToController,
} from '../../../../utils/initialization-utils';
import {randomID} from '../../../../utils/utils';
import {getAttributesFromLinkSlot} from '../../../common/result-link/attributes-slot';
import {SmartSnippetSuggestionCommon} from '../../../common/smart-snippets/atomic-smart-snippet-suggestions/smart-snippet-suggestions-common';
import {Bindings} from '../../atomic-search-interface/atomic-search-interface';

Expand All @@ -30,6 +31,9 @@ import {Bindings} from '../../atomic-search-interface/atomic-search-interface';
* </atomic-smart-snippet-suggestions>
* ```
*
* @slot source-anchor-attributes - Lets you pass [attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attributes) down to anchor elements, overriding other attributes.
* To be used exclusively in anchor elements, such as: `<a slot="source-anchor-attributes" target="_blank"></a>`.
*
* @part container - The wrapper with a border around the entire component.
* @part heading - The heading above the list of related questions.
* @part questions - The list of related questions.
Expand Down Expand Up @@ -91,6 +95,8 @@ export class AtomicSmartSnippetSuggestions implements InitializableComponent {

this.smartSnippetSuggestionListCommon = new SmartSnippetSuggestionCommon({
id: this.id,
getSourceAnchorAttributes: () =>
getAttributesFromLinkSlot(this.host, 'source-anchor-attributes'),
getHost: () => this.host,
getBindings: () => this.bindings,
getHeadingLevel: () => this.headingLevel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
BindStateToController,
} from '../../../../utils/initialization-utils';
import {randomID} from '../../../../utils/utils';
import {getAttributesFromLinkSlot} from '../../../common/result-link/attributes-slot';
import {SmartSnippetCommon} from '../../../common/smart-snippets/atomic-smart-snippet/smart-snippet-common';
import {Bindings} from '../../atomic-search-interface/atomic-search-interface';

Expand All @@ -29,6 +30,9 @@ import {Bindings} from '../../atomic-search-interface/atomic-search-interface';
* </atomic-smart-snippet>
* ```
*
* @slot source-anchor-attributes - Lets you pass [attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attributes) down to anchor elements, overriding other attributes.
* To be used exclusively in anchor elements, such as: `<a slot="source-anchor-attributes" target="_blank"></a>`.
*
* @part smart-snippet - The wrapper of the entire smart snippet.
* @part question - The header displaying the question that is answered by the found document excerpt.
* @part answer - The container displaying the full document excerpt.
Expand Down Expand Up @@ -102,6 +106,8 @@ export class AtomicSmartSnippet implements InitializableComponent {
this.smartSnippetCommon = new SmartSnippetCommon({
id: this.id,
modalTagName: 'atomic-smart-snippet-feedback-modal',
getSourceAnchorAttributes: () =>
getAttributesFromLinkSlot(this.host, 'source-anchor-attributes'),
getHost: () => this.host,
getBindings: () => this.bindings,
getModalRef: () => this.modalRef,
Expand Down
20 changes: 18 additions & 2 deletions packages/atomic/src/pages/examples/ipx.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@
top: 10px;
left: 5px;
}
atomic-did-you-mean,
atomic-notifications,
atomic-smart-snippet,
atomic-smart-snippet-suggestions {
padding-bottom: 1rem;
}

atomic-smart-snippet::part(source-url),
atomic-smart-snippet-suggestions::part(source-url) {
word-wrap: break-word;
}
</style>
</head>

Expand Down Expand Up @@ -231,8 +242,13 @@
<atomic-notifications></atomic-notifications>
</atomic-layout-section>
<atomic-layout-section section="results">
<atomic-smart-snippet></atomic-smart-snippet>
<atomic-smart-snippet-suggestions></atomic-smart-snippet-suggestions>
<atomic-generated-answer></atomic-generated-answer>
<atomic-smart-snippet>
<a slot="source-anchor-attributes" target="_blank" rel="noopener"></a>
</atomic-smart-snippet>
<atomic-smart-snippet-suggestions>
<a slot="source-anchor-attributes" target="_blank" rel="noopener"></a>
</atomic-smart-snippet-suggestions>
<atomic-result-list image-size="none">
<atomic-result-template>
<template>
Expand Down

0 comments on commit 8a9342a

Please sign in to comment.