Skip to content

Commit

Permalink
added TomsterPopper component to test keyboard event handling
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelchadwick committed Jun 26, 2024
1 parent c597a69 commit 237218d
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/components/tomster-popper.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div id="tomster-popper" class={{if this.isVisible 'show' 'hide'}}>
<div id="greeting">
{{t "general.greeting" name="Tomster"}}
</div>
<img
src="/assets/images/teaching-tomster.png"
alt={{t "general.teachingTomster"}}
{{on-key 'ctrl+shift+KeyT' this.popToggle event='keyup'}}
/>
</div>
14 changes: 14 additions & 0 deletions app/components/tomster-popper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';

export default class TomsterPopperComponent extends Component {
@tracked isVisible = false;

@action
popToggle() {
this.isVisible = !this.isVisible;

console.log(`tomster says...${this.isVisible ? 'hello' : 'goodbye'}`);
}
}
1 change: 1 addition & 0 deletions app/styles/components.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
@import "components/messages";
@import "components/new-message-input";
@import "components/ribbon";
@import "components/tomster-popper";
62 changes: 62 additions & 0 deletions app/styles/components/tomster-popper.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#tomster-popper {
bottom: -100vw;
display: flex;
flex-direction: column;
justify-content: center;
left: 50%;
position: fixed;
z-index: 10;
}
#tomster-popper.show {
animation-duration: 500ms;
animation-name: slidein;
bottom: -70px;
display: flex;
}
#tomster-popper.hide {
animation-duration: 1s;
animation-name: slideout;
bottom: -100vw;
display: flex;
}
#tomster-popper #greeting {
background-color: #fff;
border: 1px solid #000;
border-radius: 5px;
bottom: 5px;
padding: 5px;
position: relative;
width: 140px;
}
#tomster-popper img {
height: 200px;
width: 200px;
}

@keyframes slidein {
from {
bottom: -100vw;
translate: 0 10vw;
scale: 50% 50%;
}

to {
bottom: -70px;
translate: 0 0;
scale: 100% 1;
}
}

@keyframes slideout {
from {
bottom: -70px;
translate: 0 0;
scale: 100% 1;
}

to {
bottom: -100vw;
translate: 0 10vw;
scale: 50% 50%;
}
}
2 changes: 2 additions & 0 deletions app/templates/application.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<HeadLayout />

<div class="application-wrapper">
<TomsterPopper />

<Layout::Ribbon @position="right" @content={{this.env}} />

<header>
Expand Down
1 change: 1 addition & 0 deletions config/icons.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = function () {
'free-solid-svg-icons': [
'at',
'book',
'check',
'circle-info',
'ear-listen',
'flask',
Expand Down
18 changes: 18 additions & 0 deletions tests/acceptance/remember-stuff-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { module, test } from 'qunit';
import { click, visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'remember-stuff/tests/helpers';
import ENV from 'remember-stuff/config/environment';
// import { triggerKeyEvent } from '@ember/test-helpers';
// import { elementInView } from '../helpers/intersection-observing';

module('Acceptance | remember stuff', function (hooks) {
setupApplicationTest(hooks);
Expand Down Expand Up @@ -131,4 +133,20 @@ module('Acceptance | remember stuff', function (hooks) {
assert.dom('footer a.menu-prod').hasText('[Prod]');
}
});

test('see if tomster works', async function (assert) {
await visit('/');

assert.dom('#tomster-popper').exists();

// TODO
// assert.false(elementInView('tomster-popper'));

// await triggerKeyEvent('.application-wrapper', 'keyup', 84, {
// shiftKey: true,
// ctrlKey: true,
// });

// assert.true(elementInView('tomster-popper'));
});
});
28 changes: 28 additions & 0 deletions tests/helpers/intersection-observing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const intersection = (r1, r2) => {
const xOverlap = Math.max(0, Math.min(r1.x + r1.w, r2.x + r2.w) - Math.max(r1.x, r2.x));
const yOverlap = Math.max(0, Math.min(r1.y + r1.h, r2.y + r2.h) - Math.max(r1.y, r2.y));
const overlapArea = xOverlap * yOverlap;

return overlapArea;
};

const percentInView = (div) => {
const rect = div.getBoundingClientRect();

const dimension = { x: rect.x, y: rect.y, w: rect.width, h: rect.height };
const viewport = { x: 0, y: 0, w: window.innerWidth, h: window.innerHeight };
const divsize = dimension.w * dimension.h;
const overlap = intersection(dimension, viewport);
const percent = overlap / divsize;

return percent;
};

export function elementInView(elemId) {
console.log(
'elementInView?',
percentInView(document.getElementById(elemId)),
percentInView(document.getElementById(elemId)) > 0,
);
return percentInView(document.getElementById(elemId)) > 0;
}
28 changes: 28 additions & 0 deletions tests/integration/components/tomster-popper-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'remember-stuff/tests/helpers';
import { render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
// import { elementInView } from '../../helpers/intersection-observing';

module('Integration | Component | tomster-popper', function (hooks) {
setupRenderingTest(hooks);

test('it renders', async function (assert) {
await render(hbs`<TomsterPopper />`);

assert.dom('#tomster-popper').exists();

// TODO
// assert.false(elementInView('tomster-popper'));

// let tomster = document.querySelector('#tomster-popper');
// tomster.classList.add('show');

// assert.true(elementInView('tomster-popper'));

// tomster.classList.remove('show');
// tomster.classList.add('hide');

// assert.false(elementInView('tomster-popper'));
});
});
1 change: 1 addition & 0 deletions tests/test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as QUnit from 'qunit';
import { setApplication } from '@ember/test-helpers';
import { setup } from 'qunit-dom';
import { start } from 'ember-qunit';
import './helpers/intersection-observing';

setApplication(Application.create(config.APP));

Expand Down
1 change: 1 addition & 0 deletions translations/en-us.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ general:
status: Status
summary: Summary
sunday: Sunday
teachingTomster: Teaching Tomster
thursday: Thursday
time: Time
timezone: Timezone
Expand Down

0 comments on commit 237218d

Please sign in to comment.