Here's a self-contained HTML block for an LLM model selector component, using JavaScript for logic and Tailwind CSS for styling, loaded directly from their CDNs.
This component:
- Displays a list of LLM models using radio buttons.
- Highlights the selected model.
- Automatically selects a default model.
- Dispatches a custom
modelSelected event with the chosen model's ID, so other parts of your application can react.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LLM Model Selector</title>
<!-- Tailwind CSS CDN -->
<script src="https://cdn.tailwindcss.com"></script>
<style>
/* Optional: Custom styling for the radio button focus ring if needed, though Tailwind handles much of it */
input[type="radio"]:focus {
outline: none; /* Remove default outline */
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.5); /* Custom blue focus ring */
}
</style>
</head>
<body class="bg-gray-900 flex items-center justify-center min-h-screen p-4">
<!-- LLM Model Selector Component -->
<div id="llmModelSelector" class="bg-gray-800 p-6 rounded-lg shadow-xl text-gray-100 max-w-md w-full">
<h2 class="text-xl font-semibold mb-4 text-center">Select an LLM Model</h2>
<div class="space-y-3 mb-4">
<!-- Model Option 1 -->
<label for="gpt-4o"
class="flex items-center cursor-pointer p-3 rounded-md transition-colors duration-200
hover:bg-gray-700 has-[:checked]:bg-blue-600 has-[:checked]:text-white
has-[:checked]:ring-2 has-[:checked]:ring-blue-400">
<input type="radio" id="gpt-4o" name="llm_model" value="gpt-4o" class="sr-only">
<div class="flex items-center w-full">
<span class="w-5 h-5 border-2 rounded-full flex-shrink-0 mr-3
border-gray-500 has-[:checked]:border-white
flex items-center justify-center">
<span class="w-3 h-3 rounded-full bg-transparent has-[:checked]:bg-white"></span>
</span>
<div class="flex-grow">
<span class="font-medium">GPT-4o</span>
<p class="text-sm text-gray-400 has-[:checked]:text-blue-200">OpenAI's latest flagship model</p>
</div>
<span class="text-xs font-semibold ml-2 px-2 py-1 rounded-full bg-blue-700 has-[:checked]:bg-blue-800">New</span>
</div>
</label>
<!-- Model Option 2 -->
<label for="claude-3-opus"
class="flex items-center cursor-pointer p-3 rounded-md transition-colors duration-200
hover:bg-gray-700 has-[:checked]:bg-purple-600 has-[:checked]:text-white
has-[:checked]:ring-2 has-[:checked]:ring-purple-400">
<input type="radio" id="claude-3-opus" name="llm_model" value="claude-3-opus" class="sr-only">
<div class="flex items-center w-full">
<span class="w-5 h-5 border-2 rounded-full flex-shrink-0 mr-3
border-gray-500 has-[:checked]:border-white
flex items-center justify-center">
<span class="w-3 h-3 rounded-full bg-transparent has-[:checked]:bg-white"></span>
</span>
<div class="flex-grow">
<span class="font-medium">Claude 3 Opus</span>
<p class="text-sm text-gray-400 has-[:checked]:text-purple-200">Anthropic's most intelligent model</p>
</div>
</div>
</label>
<!-- Model Option 3 -->
<label for="gemini-1.5-pro"
class="flex items-center cursor-pointer p-3 rounded-md transition-colors duration-200
hover:bg-gray-700 has-[:checked]:bg-orange-600 has-[:checked]:text-white
has-[:checked]:ring-2 has-[:checked]:ring-orange-400">
<input type="radio" id="gemini-1.5-pro" name="llm_model" value="gemini-1.5-pro" class="sr-only">
<div class="flex items-center w-full">
<span class="w-5 h-5 border-2 rounded-full flex-shrink-0 mr-3
border-gray-500 has-[:checked]:border-white
flex items-center justify-center">
<span class="w-3 h-3 rounded-full bg-transparent has-[:checked]:bg-white"></span>
</span>
<div class="flex-grow">
<span class="font-medium">Gemini 1.5 Pro</span>
<p class="text-sm text-gray-400 has-[:checked]:text-orange-200">Google's powerful multimodal model</p>
</div>
</div>
</label>
<!-- Model Option 4 -->
<label for="llama-3-70b"
class="flex items-center cursor-pointer p-3 rounded-md transition-colors duration-200
hover:bg-gray-700 has-[:checked]:bg-green-600 has-[:checked]:text-white
has-[:checked]:ring-2 has-[:checked]:ring-green-400">
<input type="radio" id="llama-3-70b" name="llm_model" value="llama-3-70b" class="sr-only">
<div class="flex items-center w-full">
<span class="w-5 h-5 border-2 rounded-full flex-shrink-0 mr-3
border-gray-500 has-[:checked]:border-white
flex items-center justify-center">
<span class="w-3 h-3 rounded-full bg-transparent has-[:checked]:bg-white"></span>
</span>
<div class="flex-grow">
<span class="font-medium">Llama 3 70B</span>
<p class="text-sm text-gray-400 has-[:checked]:text-green-200">Meta's large open-source model</p>
</div>
</div>
</label>
</div>
<p class="text-sm text-gray-400 mt-4 text-center">Selected Model: <span id="selectedModelDisplay" class="font-bold text-gray-300"></span></p>
</div>
<!-- JavaScript for Component Logic -->
<script>
document.addEventListener('DOMContentLoaded', () => {
const llmModelSelector = document.getElementById('llmModelSelector');
const radioButtons = llmModelSelector.querySelectorAll('input[name="llm_model"]');
const selectedModelDisplay = document.getElementById('selectedModelDisplay');
// Set a default selected model
const defaultModelId = 'gpt-4o'; // Change this to your preferred default
// Function to update the display and dispatch event
const updateSelection = (selectedId) => {
const selectedRadioButton = llmModelSelector.querySelector(`input[value="${selectedId}"]`);
if (selectedRadioButton) {
selectedRadioButton.checked = true; // Ensure it's checked
selectedModelDisplay.textContent = selectedId.replace(/-/g, ' ').toUpperCase(); // Format for display
// Dispatch a custom event
const event = new CustomEvent('modelSelected', {
detail: { modelId: selectedId }
});
llmModelSelector.dispatchEvent(event);
}
};
// Initialize with the default model
updateSelection(defaultModelId);
// Add event listeners to radio buttons
radioButtons.forEach(radio => {
radio.addEventListener('change', (event) => {
updateSelection(event.target.value);
});
});
// --- Example of how to listen for the custom event ---
llmModelSelector.addEventListener('modelSelected', (event) => {
console.log('Model Selected Event Fired:', event.detail.modelId);
// You can add more logic here, e.g., enable/disable other parts of your app
// or fetch data based on the selected model.
});
// -----------------------------------------------------
});
</script>
</body>
</html>
Explanation:
-
HTML Structure:
- The main container
div with id="llmModelSelector" encapsulates the entire component.
- A
h2 for the title.
- A
div with space-y-3 to add vertical spacing between model options.
- Each model option is a
label element. This is crucial for accessibility, allowing users to click anywhere on the visible "card" to select the radio button.
input type="radio":
id and for attributes link the label to its corresponding input.
name="llm_model" groups all radio buttons together, ensuring only one can be selected at a time.
value holds the unique identifier for each model (e.g., gpt-4o).
class="sr-only" (screen reader only) hides the native radio button visual, allowing us to fully customize its appearance using the parent label.
- Custom Radio Button Indicator: The
span elements inside each label (w-5 h-5 border-2 rounded-full...) create the visual representation of the radio button (the outer circle and inner dot).
has-[:checked] is a powerful Tailwind JIT feature that allows you to style a parent element based on the state of a child element (in this case, the input:checked state). This is used to change the background, text color, and border/ring for the selected option.
selectedModelDisplay: A span to show the currently selected model to the user.
-
Tailwind CSS (via CDN):
- Loaded directly from
https://cdn.tailwindcss.com.
- Classes like
bg-gray-800, p-6, rounded-lg, shadow-xl, text-gray-100 are used for the overall container styling.
flex, items-center, justify-center, min-h-screen, max-w-md, w-full are used for layout and responsiveness.
- The
has-[:checked] utility is heavily used to style the parent label when its child radio button is checked. This creates a visually distinct selected state for the entire model option "card."
To use this, simply save the code as an .html file and open it in your web browser.