Skip to content

Package Manager

The Package Manager component provides a tabbed interface for displaying installation commands for different package managers (npm, pnpm, yarn, bun). It allows users to easily copy the correct installation command for their preferred package manager.

  • Metro UI Core
  • DOM library
  • Hooks (for ID generation)

See the Example by Serhii Pimenov on CodePen.

<div data-role="package-manager" data-package="metro4"></div>
<div data-role="package-manager"
data-package="metro4"
data-default="yarn"
data-deps="dev">
</div>
// Create a package manager component programmatically
const packageManager = Metro.makePlugin("#element", "package-manager", {
package: "metro4",
default: "npm",
deps: "dev"
});
// Alternative jQuery-style creation
const packageManager = $("<div>").packageManager({
package: "metro4",
default: "npm",
deps: "dev"
}).appendTo("#container");

The Package Manager component accepts the following configuration options:

ParameterTypeDefaultDescription
linkstring"package-manager"Name for the tabs link
defaultstring"npm"Default selected package manager (npm, pnpm, yarn, bun)
depsstring""Dependency type: empty for regular dependencies, “dev” for dev dependencies, “peer” for peer dependencies
packagestring""Package name to install
EventDescription
onPackageManagerCreateTriggered when the component is created

The Package Manager component provides the following methods:

MethodDescription
destroy()Removes the component from the DOM
const packageManager = Metro.getPlugin("#element", "package-manager");
packageManager.destroy();

The component automatically formats the correct installation command for each package manager based on the dependency type:

Package ManagerCommand Format
npmnpm i <package>
pnpmpnpm add <package>
yarnyarn add <package>
bunbun add <package>
Package ManagerCommand Format
npmnpm i -D <package>
pnpmpnpm add -D <package>
yarnyarn add -D <package>
bunbun add -D <package>
Package ManagerCommand Format
npmnpm i <package> (npm doesn’t have a specific flag for peer dependencies)
pnpmpnpm add --save-peer <package>
yarnyarn add -P <package>
bunbun add --peer <package>

The Package Manager component uses the following CSS variables for styling:

VariableDefault (Light)Dark ModeDescription
--code-backgroundInherited from themeInherited from themeBackground color for code blocks and tabs
--code-colorInherited from themeInherited from themeText color for code blocks and icons
/* Custom styling for package manager */
.custom-package-manager {
--code-background: #f5f5f5;
--code-color: #333333;
}
  • .package-manager - Base class for the component
  • .pm-command - Container for each package manager command
  • .pm-command code - Command text
  • .pm-command button - Copy button
  1. Tabbed Interface: Easily switch between different package managers
  2. Copy to Clipboard: One-click copying of installation commands
  3. Dependency Type Support: Configure for regular, development, or peer dependencies
  4. Visual Icons: Each package manager has its own icon for easy identification
  5. Responsive Design: Works well in different container sizes
  1. Place the component near package information for easy discovery
  2. Set a default package manager that matches your project’s recommended tooling
  3. Specify the correct dependency type to ensure users install packages correctly
  4. Consider adding additional context around the component to explain what the package does
  5. For packages with complex installation requirements, consider providing additional instructions