-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sfint-3272): Add ToggleActionButton component (#76)
* feat(SFINT-3272): Add ToggleActionButton component. * feat(SFINT-3272): Add ToggleActionButton tests * feat(SFINT-3272): Apply review comments * feat(SFINT-3272): Improve option names
- Loading branch information
Showing
9 changed files
with
435 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, height=device-height" /> | ||
<title>Development - Toggle Action Button</title> | ||
<link rel="stylesheet" href="../css/CoveoFullSearch.css" /> | ||
<link rel="stylesheet" href="../css/CoveoJsSearchExtensions.css" /> | ||
<script src="../js/CoveoJsSearch.Lazy.js"></script> | ||
<script src="../commonjs/CoveoJsSearchExtensions.js"></script> | ||
<script> | ||
let attachedIds = []; | ||
document.addEventListener('DOMContentLoaded', function() { | ||
Coveo.SearchEndpoint.configureSampleEndpointV2(); | ||
Coveo.init(document.body, {}); | ||
}); | ||
</script> | ||
</head> | ||
|
||
<body id="search" class="CoveoSearchInterface" data-enable-history="false" style="padding: 1em;"> | ||
<span class="CoveoAnalytics"></span> | ||
<div class="coveo-tab-section"> | ||
<a class="CoveoTab" data-id="All" data-caption="All Content"></a> | ||
</div> | ||
<div class="coveo-search-section"> | ||
<div class="CoveoSearchbox" data-enable-omnibox="true"></div> | ||
</div> | ||
|
||
<div> | ||
<h2>Toggle Button</h2> | ||
<br /> | ||
<button | ||
class="CoveoToggleActionButton" | ||
data-deactivated-icon='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 78.997 78.997" height="298.571" width="298.571"><circle r="39.499" cy="120.385" cx="84.1" transform="translate(-44.601 -80.887)"/></svg>' | ||
data-deactivated-tooltip="Normal tooltip" | ||
data-activated-tooltip="Activated tooltip" | ||
data-activated-icon='<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 78.997 78.997" height="298.571" width="298.571"><path d="M39.434 0A39.499 39.499 0 000 39.498a39.499 39.499 0 0039.498 39.5 39.499 39.499 0 0039.5-39.5A39.499 39.499 0 0039.497 0a39.499 39.499 0 00-.064 0zm.59 7.273a31.948 31.948 0 0131.948 31.949A31.948 31.948 0 0140.023 71.17 31.948 31.948 0 018.075 39.222 31.948 31.948 0 0140.023 7.273z"/></svg>' | ||
></button> | ||
</div> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
@import './ActionButton.scss'; | ||
@import '../../sass/Variables.scss'; | ||
|
||
$activated-color: $calypso; | ||
|
||
button.CoveoActionButton.coveo-actionbutton.coveo-toggleactionbutton-activated { | ||
background-color: $activated-color; | ||
border-color: $activated-color; | ||
|
||
.coveo-actionbutton_icon { | ||
svg { | ||
fill: $primary-color-lightest; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
import { ComponentOptions, IResultsComponentBindings, Component, Initialization } from 'coveo-search-ui'; | ||
import { ActionButton } from './ActionButton'; | ||
|
||
export interface IToggleActionButtonOptions { | ||
activatedIcon: string; | ||
activatedTooltip: string; | ||
deactivatedIcon: string; | ||
deactivatedTooltip: string; | ||
click?: () => void; | ||
activate?: () => void; | ||
deactivate?: () => void; | ||
} | ||
|
||
export class ToggleActionButton extends Component { | ||
static ID = 'ToggleActionButton'; | ||
static ACTIVATED_CLASS_NAME = 'coveo-toggleactionbutton-activated'; | ||
|
||
static options: IToggleActionButtonOptions = { | ||
/** | ||
* Specifies the button icon when the button is activated. | ||
* | ||
* Default is the empty string. | ||
* | ||
* For example, with this SVG markup: | ||
* | ||
* ```xml | ||
* <svg width="1em" height="1em">...</svg> | ||
* ``` | ||
* | ||
* The attribute would be set like this: | ||
* | ||
* ```html | ||
* <button class='CoveoToggleActionButton' data-activated-icon='<svg width="1em" height="1em">...</svg>'></button> | ||
* ``` | ||
*/ | ||
activatedIcon: ComponentOptions.buildStringOption(), | ||
|
||
/** | ||
* Specifies the button tooltip when the button is activated. | ||
* | ||
* Default is the empty string. | ||
* | ||
* ```html | ||
* <button class='CoveoToggleActionButton' data-activated-tooltip='My activated button tooltip'></button> | ||
* ``` | ||
*/ | ||
activatedTooltip: ComponentOptions.buildStringOption(), | ||
|
||
/** | ||
* Specifies the button SVG icon when the button is deactivated. | ||
* Note: The SVG markup has to be HTML encoded when set using the HTML attributes. | ||
* | ||
* Default is the empty string. | ||
* | ||
* For example, with this SVG markup: | ||
* | ||
* ```xml | ||
* <svg width="1em" height="1em">...</svg> | ||
* ``` | ||
* | ||
* The attribute would be set like this: | ||
* | ||
* ```html | ||
* <button class='CoveoToggleActionButton' data-deactivated-icon='<svg width="1em" height="1em">...</svg>'></button> | ||
* ``` | ||
*/ | ||
deactivatedIcon: ComponentOptions.buildStringOption(), | ||
|
||
/** | ||
* Specifies the button tooltip text when the button is deactivated. | ||
* | ||
* Default is the empty string. | ||
* | ||
* ```html | ||
* <button class='CoveoToggleActionButton' data-deactivated-tooltip='My button tooltip'></button> | ||
* ``` | ||
*/ | ||
deactivatedTooltip: ComponentOptions.buildStringOption(), | ||
|
||
/** | ||
* Specifies the handler called when the button is clicked. | ||
* | ||
* Default is `null`. | ||
* | ||
* This option is set in JavaScript when initializing the component. | ||
*/ | ||
click: ComponentOptions.buildCustomOption(s => null), | ||
|
||
/** | ||
* Specifies the handler called when the button is activated. | ||
* | ||
* Default is `null`. | ||
* | ||
* This option is set in JavaScript when initializing the component. | ||
*/ | ||
activate: ComponentOptions.buildCustomOption(s => null), | ||
|
||
/** | ||
* Specifies the handler called when the button is deactivated. | ||
* | ||
* Default is `null`. | ||
* | ||
* This option is set in JavaScript when initializing the component. | ||
*/ | ||
deactivate: ComponentOptions.buildCustomOption(s => null) | ||
}; | ||
|
||
private _isActivated: boolean = false; | ||
private innerActionButton: ActionButton; | ||
|
||
constructor(public element: HTMLElement, public options: IToggleActionButtonOptions, public bindings?: IResultsComponentBindings) { | ||
super(element, ToggleActionButton.ID, bindings); | ||
this.options = ComponentOptions.initComponentOptions(element, ToggleActionButton, options); | ||
|
||
this.createInnerButton(bindings); | ||
} | ||
|
||
/** | ||
* Indicates whether the toggle button is in the activated state. | ||
*/ | ||
public isActivated(): boolean { | ||
return this._isActivated; | ||
} | ||
|
||
/** | ||
* Sets the toggle button to the specified state. | ||
* @param activated Whether the button is activated. | ||
*/ | ||
public setActivated(activated: boolean): void { | ||
if (activated !== this.isActivated()) { | ||
this._isActivated = activated; | ||
this.updateButton(); | ||
|
||
if (this._isActivated && this.options.activate) { | ||
this.options.activate(); | ||
} | ||
if (!this._isActivated && this.options.deactivate) { | ||
this.options.deactivate(); | ||
} | ||
} | ||
} | ||
|
||
protected onClick(): void { | ||
this.setActivated(!this.isActivated()); | ||
|
||
if (this.options.click) { | ||
this.options.click(); | ||
} | ||
} | ||
|
||
private createInnerButton(bindings?: IResultsComponentBindings): void { | ||
this.innerActionButton = new ActionButton( | ||
this.element, | ||
{ | ||
icon: this.options.deactivatedIcon, | ||
tooltip: this.options.deactivatedTooltip, | ||
click: () => this.onClick() | ||
}, | ||
bindings | ||
); | ||
|
||
this.updateButton(); | ||
} | ||
|
||
private updateButton() { | ||
if (this._isActivated) { | ||
this.element.classList.add(ToggleActionButton.ACTIVATED_CLASS_NAME); | ||
this.element.setAttribute('aria-pressed', 'true'); | ||
|
||
this.innerActionButton.updateIcon(this.options.activatedIcon); | ||
this.innerActionButton.updateTooltip(this.options.activatedTooltip); | ||
} else { | ||
this.element.classList.remove(ToggleActionButton.ACTIVATED_CLASS_NAME); | ||
this.element.setAttribute('aria-pressed', 'false'); | ||
|
||
this.innerActionButton.updateIcon(this.options.deactivatedIcon); | ||
this.innerActionButton.updateTooltip(this.options.deactivatedTooltip); | ||
} | ||
} | ||
} | ||
|
||
Initialization.registerAutoCreateComponent(ToggleActionButton); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.