Skip to content

Commit

Permalink
Add clickable component
Browse files Browse the repository at this point in the history
  • Loading branch information
maxatdetroit committed Nov 9, 2023
1 parent d7373d5 commit 8b4dbac
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/components/atoms/Clickable/Clickable.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.btn {
--bs-btn-padding-x: 0;
--bs-btn-padding-y: 0;
}
61 changes: 61 additions & 0 deletions src/components/atoms/Clickable/Clickable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import styles from '!!raw-loader!./Clickable.css';
import varStyles from '!!raw-loader!../../../shared/variables.css';
import bootstrapStyles from '!!raw-loader!../../../shared/themed-bootstrap.css';

const template = document.createElement('template');

// A space for some clickable content.
template.innerHTML = `
<slot></slot>
`;

export default class Clickable extends HTMLElement {
constructor() {
// Always call super constructor.
super();

// Create a shadow root.
this.attachShadow({ mode: 'open' });
}

connectedCallback() {
this.shadowRoot.addEventListener('slotchange', (ev) => {
const elements = ev.target.assignedElements();
elements.forEach((element) => {
if (typeof element.setIsClickable === 'function') {
const btnColor = this.getAttribute('data-btn-color');
element.setIsClickable(true, btnColor);
}
});
this.clickable.append(...elements);
});

// Create component.
this.clickable = document.createElement('a');

// Add styles.
const bootStyles = document.createElement('style');
bootStyles.textContent = bootstrapStyles;
const variableStyles = document.createElement('style');
variableStyles.textContent = varStyles;
const itemStyles = document.createElement('style');
itemStyles.textContent = styles;
this.shadowRoot.appendChild(bootStyles);
this.shadowRoot.appendChild(variableStyles);
this.shadowRoot.appendChild(itemStyles);

// Set attributes on node.
const href = this.getAttribute('href');
this.clickable.setAttribute('href', href);
const target = this.getAttribute('target');
this.clickable.setAttribute('target', target);

// Set bootstrap classes.
const btnColor = this.getAttribute('data-btn-color');
this.clickable.classList.add('btn', btnColor);

// Add slot to clickable area.
this.clickable.appendChild(template.content.cloneNode(true));
this.shadowRoot.appendChild(this.clickable);
}
}
3 changes: 3 additions & 0 deletions src/components/atoms/Clickable/cod-clickable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Clickable from './Clickable';

customElements.define('cod-clickable', Clickable);
44 changes: 44 additions & 0 deletions src/stories/clickable.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { html } from 'lit-html';
import '../components/atoms/Clickable/cod-clickable';
import '../components/organisms/Card/cod-card';
import '../components/atoms/CardIconContainer/cod-card-icon-container';
import '../components/atoms/Icon/cod-icon';
import '../components/atoms/CardBody/cod-card-body';

export default {
title: 'Components/Atoms/Clickable',
};

export const ActionButton = () => html`
<cod-clickable href="https://example.com" target="_blank" data-btn-color="btn-primary">
<cod-card
data-id="card-as-action-button"
data-width="18em"
>
<cod-card-icon-container>
<cod-icon data-icon="house" data-size="x-large" />
</cod-card-icon-container>
<cod-card-body>
<h5 class="card-title">Do Something</h5>
<p class="card-text">Like click on this card.</p>
</cod-card-body>
</cod-card>
</clickable>
`;

export const ActionButtonSuccess = () => html`
<cod-clickable href="https://example.com" target="_blank" data-btn-color="btn-success">
<cod-card
data-id="card-as-action-button"
data-width="18em"
>
<cod-card-icon-container>
<cod-icon data-icon="house" data-size="x-large" />
</cod-card-icon-container>
<cod-card-body>
<h5 class="card-title">Do Something</h5>
<p class="card-text">Like click on this card.</p>
</cod-card-body>
</cod-card>
</clickable>
`;

0 comments on commit 8b4dbac

Please sign in to comment.