Skip to content

Commit

Permalink
Create highlightElement utility (#538)
Browse files Browse the repository at this point in the history
* Create `highlightElement` utility

* Update highlight-element.ts
  • Loading branch information
jeffdaley authored Jan 17, 2024
1 parent d6247c7 commit 18c94e6
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
26 changes: 2 additions & 24 deletions web/app/components/document/sidebar/related-resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { assert } from "@ember/debug";
import HermesFlashMessagesService from "hermes/services/flash-messages";
import { FLASH_MESSAGES_LONG_TIMEOUT } from "hermes/utils/ember-cli-flash/timeouts";
import updateRelatedResourcesSortOrder from "hermes/utils/update-related-resources-sort-order";
import highlightElement from "hermes/utils/ember-animated/highlight-element";
import scrollIntoViewIfNeeded from "hermes/utils/scroll-into-view-if-needed";

export interface DocumentSidebarRelatedResourcesComponentArgs {
Expand Down Expand Up @@ -260,30 +261,7 @@ export default class DocumentSidebarRelatedResourcesComponent extends Component<
});
});

const highlight = document.createElement("div");
highlight.classList.add("highlight-affordance");
target.insertBefore(highlight, target.firstChild);

const fadeInAnimation = highlight.animate(
[{ opacity: 0 }, { opacity: 1 }],
{ duration: 50 },
);

await timeout(Ember.testing ? 0 : 2000);

const fadeOutAnimation = highlight.animate(
[{ opacity: 1 }, { opacity: 0 }],
{ duration: Ember.testing ? 50 : 400 },
);

try {
await fadeInAnimation.finished;
await fadeOutAnimation.finished;
} finally {
fadeInAnimation.cancel();
fadeOutAnimation.cancel();
highlight.remove();
}
highlightElement(target);
});
},
);
Expand Down
38 changes: 38 additions & 0 deletions web/app/utils/ember-animated/highlight-element.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Ember from "ember";
import { timeout } from "ember-concurrency";

/**
* Adds a temporary highlight to a relatively positioned element.
* Used to draw attention to an item, such as a related resource
* that's been added or modified.
*/

export default async function highlightElement(target: Element) {
const highlight = document.createElement("div");

highlight.setAttribute("aria-hidden", "true");
highlight.classList.add("highlight-affordance");

target.appendChild(highlight);

const fadeInAnimation = highlight.animate([{ opacity: 0 }, { opacity: 1 }], {
duration: Ember.testing ? 0 : 50,
fill: "forwards",
});

await timeout(Ember.testing ? 0 : 2000);

const fadeOutAnimation = highlight.animate([{ opacity: 1 }, { opacity: 0 }], {
duration: Ember.testing ? 0 : 400,
fill: "forwards",
});

try {
await fadeInAnimation.finished;
await fadeOutAnimation.finished;
} finally {
fadeInAnimation.cancel();
fadeOutAnimation.cancel();
highlight.remove();
}
}

0 comments on commit 18c94e6

Please sign in to comment.