Skip to content

Charms

The Charms component provides a panel that can be positioned on any side of the screen (right, left, top, bottom) and can be opened or closed with smooth transition effects. It’s commonly used for displaying additional options, notifications, or contextual information.

<!-- Basic charms panel (right side by default) -->
<div data-role="charms" id="my-charms">
<!-- Content goes here -->
</div>
<!-- Charms panel with custom position -->
<div data-role="charms" data-position="left">
<!-- Content goes here -->
</div>
<!-- Charms panel with custom opacity -->
<div data-role="charms" data-opacity="0.8">
<!-- Content goes here -->
</div>
// Open charms
Metro.charms.open('#my-charms');
// Close charms
Metro.charms.close('#my-charms');
// Toggle charms state
Metro.charms.toggle('#my-charms');
// Close all open charms
Metro.charms.closeAll();
// Check if charms is open
const isOpen = Metro.charms.isOpen('#my-charms');
// Set opacity
Metro.charms.opacity('#my-charms', 0.8);

The charms component supports two special elements:

<!-- Charm tile -->
<div class="charm-tile">
<span class="icon mif-home"></span>
<span class="caption">Home</span>
</div>
<!-- Charm notification -->
<div class="charm-notify">
<span class="icon mif-bell"></span>
<span class="title">Notification Title</span>
<span class="content">Notification content goes here</span>
<span class="secondary">Additional information</span>
</div>

The Charms component accepts the following configuration options:

ParameterTypeDefaultDescription
charmsDeferrednumber0Deferred initialization time in milliseconds
positionstring”right”Position of the charms panel (“right”, “left”, “top”, “bottom”)
opacitynumber1Opacity of the charms panel background (0-1)
clsCharmsstring""Additional CSS class(es) for the charms element
onCharmCreatefunctionMetro.noopCallback function triggered after charms creation
onOpenfunctionMetro.noopCallback function triggered when charms is opened
onClosefunctionMetro.noopCallback function triggered when charms is closed
onTogglefunctionMetro.noopCallback function triggered when charms state is toggled

These methods can be called on a charms component instance:

// Get component instance
const charms = Metro.getPlugin('#my-charms', 'charms');
// Call methods
charms.open();
charms.close();
charms.toggle();
charms.opacity(0.8);
MethodParametersReturnDescription
open--Opens the charms panel
close--Closes the charms panel
toggle--Toggles the open/closed state of the charms panel
opacityvaluecurrent opacityGets or sets the opacity of the charms panel background
changeOpacity--Changes opacity based on data-opacity attribute
destroy-elementDestroys the component and returns the element

These methods can be called directly from the Metro.charms object:

MethodParametersReturnDescription
checkelementbooleanChecks if element is a charms component
isOpenelementbooleanChecks if charms panel is open
openelement-Opens the charms panel
closeelement-Closes the charms panel
toggleelement-Toggles the charms panel state
closeAll--Closes all charms panels
opacityelement, opacity-Sets opacity for a charms panel

The Charms component uses the following LESS variables for styling:

VariableDefault (Light)Dark ModeDescription
@zindex-charms1090-Z-index of the charms component
@dark#1d1d1d-Default background color
@white#ffffff-Default text color
@transition-transform.3s ease-Transition effect for opening/closing
/* Custom styling for a specific charms panel */
#my-charms {
background-color: rgba(0, 0, 0, 0.8);
color: white;
}
  • .charms - Main container class (automatically added)
  • .charm-tile - Tile element within charms
  • .charm-notify - Notification element within charms
  • .top-side - Positions charms at the top (automatically added based on position)
  • .bottom-side - Positions charms at the bottom (automatically added based on position)
  • .left-side - Positions charms at the left (automatically added based on position)
  • .right-side - Positions charms at the right (automatically added based on position)
  • .open - Applied when charms is open
  • .charm-tile .icon - Icon within a charm tile
  • .charm-tile .caption - Caption text within a charm tile
  • .charm-notify .icon - Icon within a charm notification
  • .charm-notify .title - Title within a charm notification
  • .charm-notify .content - Content within a charm notification
  • .charm-notify .secondary - Secondary text within a charm notification
  1. Use charms panels sparingly to avoid overwhelming the user interface
  2. Consider the appropriate position based on your application’s layout
  3. Use appropriate opacity to ensure content remains readable
  4. Provide clear ways for users to close the charms panel
  5. Use charm-tile and charm-notify elements for consistent styling