top of page
Writer's pictureThe Tech Platform

Using in-element in Ember.js

Updated: Feb 21

Ember.js, renowned for its robust framework and developer-friendly environment, continues to evolve with innovative features aimed at enhancing productivity and flexibility. Among these features, the in-element helper stands out as a versatile tool that opens new possibilities for rendering elements within Ember.js applications. In this article, we delve into the intricacies of utilizing the in-element helper and explore its myriad applications in Ember.js development.


In this complete guide, we'll learn all about the in-element helper in Ember.js. We'll begin by understanding its basic ideas and how to use it. Then, we'll move on to seeing how it works with real examples and situations you might encounter. By the time you finish reading, you'll understand how to use the in-element helper effectively and be able to make the most out of it for your Ember.js projects, taking them to the next level.


Using in-element in Ember.js


Building a Feature: Adding Song Deletion Functionality

In "Rock and Roll with Ember Octane," we develop an application where users can create bands and songs. However, the current functionality cannot delete songs.


Goal:

Our goal is to enhance the app by allowing users to delete songs, ensuring they confirm this action before proceeding.



UI Flow Overview:

Hovering trash can icon: When a user hovers over a song list item, a trash can icon should appear.


Clicking the trash can: Clicking the trash can icon prompts a deletion confirmation dialog.


Handling user actions: The deletion confirmation dialog allows users to either confirm or cancel the deletion.

  • If the user cancels or clicks outside the dialog, the operation is aborted.

  • If the user confirms deletion, the song is deleted, and the dialog closes.


Implementing Hover Functionality with TailwindCSS

TailwindCSS is a utility-first CSS framework that allows for rapid development by providing pre-defined classes for common styling needs. In our application, TailwindCSS is utilized to style the list of songs.


Adding Hover Effects for the Trash Can Icon:

To enhance the user experience, we want to display a trash can icon when a user hovers over a song item in the list. This will indicate to the user that they can delete the song by clicking on the trash can icon.


Configuring TailwindCSS for Group-hover Functionality:

TailwindCSS supports "group hover" functionality, where elements within a group can have different hover states based on the hover state of the parent group. We'll utilize this feature to ensure that the trash can icon appears not only when the direct parent is hovered but also when the song item itself is hovered.


Initial Setup:

Our application's template file, songs.hbs, contains a list of songs rendered using an each loop.


Each song item is structured with appropriate HTML markup, including the title, rating, and a trash can icon (which will be added).


Adding Hover Effects:

We begin by adding a background hover effect for each song item to provide visual feedback to users when hovering over a song.

{{!-- songs.hbs --}} 
<ul> 
	{{#each this.sortedSongs as |song|}} 
		<li class="mb-2 hover:bg-blue-700"> 
			<div class="mb-2 px-2 py-1"> 
				{{capitalize song.title}} 
				<span class="float-right"> 
				<StarRating 
					@rating={{song.rating}} 
					@onUpdate={{fn this.updateRating song}} 
				/> 
				</span> 
			</div> 
		</li> 
	{{/each}} 
</ul>

Implementing Trash Can Icon on Hover:

We add the trash can icon within a button element and ensure it's only visible when the song item or its parent is hovered.


The button is configured to trigger the deletion process upon click.

{{!-- songs.hbs --}} 
<ul> 
	{{#each this.sortedSongs as |song|}} 
		<li class="-mx-2 hover:bg-blue-700 cursor-pointer group hover:bg-blue-700"> 
			<div class="mb-2 px-2 py-1"> 
			<button 
				type="button" 
				class="inline-block pr-2 text-transparent group-hover:text-current" 
				{{on "click" (set this.songToDelete song)}} 
			> 

			<FaIcon 
				@icon="trash-alt" 
				@prefix="far" 
			/> 
			</button> 
			{{capitalize song.title}} 
			<span class="float-right"> 
			<StarRating 
				@rating={{song.rating}} 
				@onUpdate={{fn this.updateRating song}} 
			/> 
			</span> 
			</div> 
		</li> 
	{{/each}} 
</ul>

Configuring TailwindCSS for Group-hover:

We update the TailwindCSS configuration to enable the group-hover variant for the textColor category, ensuring the trash can icon changes color when hovered over.

// tailwind.config.js 
module.exports = { 
	purge: [], 
	theme: { 
		extend: {}, 
	}, 
	variants: { 
		textColor: ['responsive', 'hover', 'focus', 'group-hover'], 
	}, 
	plugins: [], 
};

Pointing CSS Tool to TailwindCSS Config:

Finally, we ensure that our CSS tool (e.g., PostCSS) is configured to use the updated TailwindCSS configuration file.

// ember-cli-build.js 
let app = new EmberApp(defaults, { 
	postcssOptions: { 
		compile: { 
			plugins: [ 
			require('tailwindcss')('./tailwind.config.js') 
			] 
		} 
	} 
});

With these implementations, the trash can icon will now appear in front of each song item when hovered over, providing users with the option to delete songs.




Adding Deletion Confirmation Dialog

In software development, a modal dialog is a graphical control element that appears on top of the main content to prompt the user for information or to confirm an action before proceeding. In our case, we want to implement a modal dialog to confirm the deletion of a song in the Ember Octane application.


Conditionally Displaying the Dialog:

We only want to show the deletion confirmation dialog when the user intends to delete a song. Therefore, we conditionally render the dialog based on the value of this.songToDelete.


Markup and Structure:

The deletion confirmation dialog consists of two main components: the overlay and the panel.


The overlay is a semi-transparent background covering the entire screen, which allows users to click outside the dialog to cancel the deletion.


The panel contains the actual confirmation message, along with buttons to confirm or cancel the deletion.

{{!-- songs.hbs --}}  
{{#if this.songToDelete}}  
 <div class="fixed bottom-0 inset-x-0 px-4 pb-4 sm:inset-0 sm:flex sm:items-center sm:justify-center">  
 {{!-- Overlay --}}  
 <div  
 class="fixed inset-0 transition-opacity"  
 role="button"  
 {{on "click" (set this.songToDelete null)}}  
 > 
 <div class="absolute inset-0 bg-gray-500 opacity-75"></div> 
 </div> 

  {{!-- Panel --}} 
 <div class="bg-white rounded-lg px-4 pt-5 pb-4 overflow-hidden shadow-xl transform sm:max-w-lg sm:w-full sm:p-6" role="dialog" aria-modal="true" aria-labelledby="modal-headline"> 
 <div class="sm:flex sm:items-start"> 
 <div class="mx-auto flex-shrink-0 flex items-center justify-center h-12 w-12 rounded-full bg-red-100 sm:mx-0 sm:h-10 sm:w-10"> 
 <svg class="h-6 w-6 text-red-600" fill="none" viewBox="0 0 24 24" stroke="currentColor"> 
 <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/> 
 </svg> 
 </div> 
 <div class="mt-3 text-center sm:mt-0 sm:ml-4 sm:text-left"> 
 <h3 class="text-lg leading-6 font-medium text-gray-900"> 23            Delete song 
 </h3> 
 <div class="mt-2"> 
 <p class="text-sm leading-5 text-gray-500"> 
              Are you sure you want to delete {{this.songToDelete.title}}? 
 </p> 
 </div> 
 </div> 
 </div> 
 <div class="mt-5 sm:mt-4 sm:flex sm:flex-row-reverse"> 
 <span class="flex w-full rounded-md shadow-sm sm:ml-3 sm:w-auto"> 
 <button 
 type="button" 
 class="inline-flex justify-center w-full rounded-md border border-transparent px-4 py-2 bg-red-600 leading-6 font-medium text-white shadow-sm hover:bg-red-500" 
 {{on "click" this.deleteSong}} 
 > 
            Delete 
 </button> 
 </span> 
 <span class="mt-3 flex w-full rounded-md shadow-sm sm:mt-0 sm:w-auto"> 
 <button 
 type="button" 
 class="inline-flex justify-center w-full rounded-md border border-gray-300 px-4 py-2 bg-white leading-6 font-medium text-gray-700 shadow-sm hover:text-gray-500" 
 {{on "click" (set this.songToDelete null)}} 
 > 
             Cancel 
 </button> 
  </span> 
 </div> 
  </div> 
 </div> 
{{/if}} 

Handling User Actions (Delete or Cancel):

When the user clicks the "Delete" button, we trigger the deleteSong action, which deletes the song and resets the state (this.songToDelete).


If the user clicks the "Cancel" button or clicks outside the dialog (on the overlay), we simply reset the state (this.songToDelete) to close the dialog.


Resetting the State after Deletion or Cancellation:

In the controller (songs.js), we define the deleteSong action to delete the song and reset this.songToDelete to null.


After deletion, we fetch the updated list of songs to refresh the UI.

// songs.js  
import Controller from '@ember/controller';  
import { tracked } from '@glimmer/tracking';  
import { action } from '@ember/object';  
  
export default class BandsBandSongsController extends Controller {  
 // (...)  
 @tracked songToDelete = null;  
 @action 
  async deleteSong() { 
    await this.catalog.delete('song', this.songToDelete); 
 this.catalog.fetchRelated(this.model, 'songs'); 
 this.songToDelete = null; 
} 
} 

With these implementations, users can now confirm the deletion of a song through the modal dialog, ensuring a more deliberate and controlled deletion process.




Integrating in-element Helper for Rendering Modal Dialog

The in-element helper is a powerful tool in Ember.js that allows developers to render a template block in a DOM element outside of its original context. This is particularly useful for scenarios like rendering modal dialogs in a top-level container, providing better control over DOM placement and improving accessibility.


Creating a Modal Container in the Application Template:

To utilize the in-element helper effectively, we first need to create a container in the application template where the modal dialog will be rendered.


We'll add a <div> element with a unique ID to serve as the modal container:

{{!-- application.hbs --}} 
<div id="modal-container"> 
</div> 

Using the in-element Helper to Render the Modal Dialog:

With the modal container in place, we can now use the in-element helper to render our modal dialog within the designated container. We wrap the modal dialog block with the in-element helper, passing the modal container as the parameter:

{{!-- application.hbs --}} 
{{!-- ... --}} 
{{#in-element this.modalContainer}} 
 {{#if this.songToDelete}} 
 {{!-- ... --}} 
 {{/if}} 
{{/in-element}} 

Defining the modalContainer Property in the Controller:

To ensure that the in-element helper knows where to render the modal dialog, we define a modalContainer property in the controller. This property should return the DOM element corresponding to the modal container.


We achieve this by using document.getElementById() to retrieve the element based on its ID:

// songs.js 
export default class BandsBandSongsController extends Controller { 
 // (...) 
  get modalContainer() { 
 return document.getElementById('modal-container'); 
  } 
} 


By integrating the in-element helper into our Ember.js application, we can effectively render modal dialogs in a separate container, improving code organization and maintainability. Additionally, this approach offers greater flexibility and control over the presentation of modal dialogs to enhance the user experience.


Exploring Further Applications of in-element Helper

The in-element helper serves as a powerful tool in Ember.js for rendering elements outside of the current UI context. Its versatility extends beyond modal dialogs, offering various possibilities for enhancing user experience and interface functionality. Let's delve into some of the potential applications of the in-element helper:


Rendering Elements Outside of the Current UI Context:

  • One of the primary applications of the in-element helper is rendering elements outside of the current UI context. This allows developers to display content in positions that are not directly governed by the current template structure.

  • For example, you might want to render a notification banner at the top of the page to inform users of important updates without interrupting their current workflow. The in-element helper enables such notifications to be displayed independently of the current UI components.


Examples of Use Cases:

  • Flash Messages: Flash messages are transient notifications displayed to users to provide feedback or alerts about certain actions. By utilizing the in-element helper, developers can render flash messages dynamically at specific locations on the page, ensuring they catch the user's attention without disrupting the flow of the application.

  • Sidebars: Sidebars are commonly used for navigation, displaying additional information, or offering contextual actions. With the in-element helper, developers can render sidebars outside of the main content area, allowing for flexible placement and dynamic content updates.

  • Chat Widgets: Chat widgets provide real-time communication capabilities within an application. By leveraging the in-element helper, developers can render chat interfaces outside of the primary UI structure, ensuring seamless integration while maintaining accessibility and responsiveness.


Mention of ember-elsewhere Add-on for Enhanced Flexibility:

  • While the in-element helper provides significant flexibility, developers seeking additional features and customization options may benefit from exploring the ember-elsewhere add-on.

  • Ember-elsewhere extends the capabilities of rendering elements outside of the current context by offering enhanced flexibility and control. It provides a comprehensive set of tools for managing component placement, handling updates, and ensuring compatibility across different Ember.js versions.

  • Developers looking to implement complex UI patterns or integrate third-party components seamlessly into their Ember.js applications can leverage ember-elsewhere to streamline the development process and achieve optimal results.


In summary, the in-element helper offers a versatile solution for rendering elements outside of the current UI context in Ember.js applications. Whether it's displaying notifications, sidebars, or chat widgets, the in-element helper empowers developers to enhance user experience and interface functionality effectively. For those seeking advanced features and customization options, exploring the ember-elsewhere add-on can further extend the capabilities of managing out-of-context elements within Ember.js applications.

0 comments

Recent Posts

See All

Comments


bottom of page