Reusable Button Variants
ReactJS
Easy
5 views
Problem Description
Create a Button component with variants: primary and ghost.
Output Format
Render a React component.
Constraints
Use a single component with a variant prop.
Official Solution
import React from 'react';
export function Button({ variant = 'primary', disabled = false, children, onClick }) {
const base = {
padding: '10px 14px',
borderRadius: 12,
border: '1px solid transparent',
fontWeight: 600,
cursor: disabled ? 'not-allowed' : 'pointer'
};
const stylesByVariant = {
primary: { background: '#0b5', color: '#fff' },
ghost: { background: 'transparent', color: '#124', border: '1px solid #ddd' }
};
const style = { ...base, ...stylesByVariant[variant], opacity: disabled ? 0.6 : 1 };
return (
<button type='button' style={style} disabled={disabled} onClick={onClick}>
{children}
</button>
);
}
export default function App() {
return (
<div style={{ display: 'flex', gap: 12, padding: 16 }}>
<Button onClick={() => {}}>Start on meetcode</Button>
<Button variant='ghost' onClick={() => {}}>View topics</Button>
</div>
);
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!