cro-components
v1.0.13
Published
A collection of reusable web components for Virgin Media O2 CRO tests, with extensible architecture for project-specific components
Maintainers
Readme
cro-components
A collection of reusable web components for CRO (Conversion Rate Optimization) tests, built with Storybook and designed for extensibility.
Table of Contents
- Installation
- Quick Start
- Creating Components
- Scripts
- Development Workflow
- Build and Export
- Testing and Linting
- Dependencies
- License
Installation
Install the package in your project:
npm install cro-componentsManual Setup (if automatic setup doesn't run)
If the automatic setup doesn't create the cro-components directory, run the setup manually:
# Option 1: Using the CLI command
npx cro-setup
# Option 2: Run the script directly
node node_modules/cro-components/postinstall.js
# Option 3: If using npm link during development
node path/to/your/cro-components/postinstall.jsThis will:
- Create the
cro-components/directory in your project root - Add the
build-cro-componentsscript to your package.json - Update your
.gitignorefile - Provide a detailed README in the components directory
Quick Start
Using Existing Core Components
Import and use the core components in your CRO tests:
// Import the component files
import './cro-component-exports/Button.js';
// Use in your HTML
const button = document.createElement('cro-button');
button.setAttribute('label', 'Click Me');
button.setAttribute('type', 'primary');
document.body.appendChild(button);Creating Your First Component
Generate a new component:
npx cro-generate MyAwesomeButtonThis creates a complete component structure with:
- Web component implementation
- Storybook stories
- Jest tests
Creating Components
Using the CLI Generator
The easiest way to create components is using the built-in CLI:
# Create a simple component
npx cro-generate ComponentName
# Create nested components for organization
npx cro-generate forms/ContactForm
npx cro-generate modals/newsletter/SignupModal
npx cro-generate buttons/social/FacebookButtonComponent Structure
Each component follows this structure:
cro-components/
├── cro-component-name/
│ ├── ComponentName.js # Web component implementation
│ ├── ComponentName.stories.js # Storybook stories
│ └── ComponentName.test.js # Jest tests
├── forms/
│ └── cro-contact-form/
│ ├── ContactForm.js
│ ├── ContactForm.stories.js
│ └── ContactForm.test.js
└── README.mdComponent Template
Here's a basic template for components:
class MyComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: "open" });
this.shadowRoot.innerHTML = `
<div class="cro-my-component">
<span>${this.getAttribute("label") || "Default Label"}</span>
</div>
`;
const style = document.createElement("style");
style.textContent = `
.cro-my-component {
padding: 16px;
border: 1px solid #ccc;
border-radius: 4px;
font-family: Arial, sans-serif;
}
`;
this.shadowRoot.append(style);
}
static get observedAttributes() {
return ["label"];
}
attributeChangedCallback(name, oldValue, newValue) {
const element = this.shadowRoot.querySelector("span");
if (name === "label" && element) {
element.textContent = newValue;
}
}
}
if (!customElements.get("cro-my-component")) {
customElements.define("cro-my-component", MyComponent);
}Scripts
Available Scripts
npm run build-cro-components- Build your components for production usenpx cro-generate <ComponentName>- Generate a new componentnpx cro-setup- Set up the CRO components directory (manual setup)npx cro-build- Build components (alternative to npm script)
Core Components Available
The package includes these core components:
- Button (
cro-button) - Configurable button component - Header (
cro-header) - Header component with customizable text - Overlay (
cro-overlay) - Full-screen overlay with header, body, and footer
Development Workflow
1. Set Up Your Project
npm install cro-components
# If setup didn't run automatically:
npx cro-setup2. Create Components
# Simple component
npx cro-generate MyButton
# Nested component for organization
npx cro-generate forms/ContactForm3. Develop and Test
- Edit your component in
cro-components/cro-my-button/ - Start Storybook to preview:
npm run storybook(if Storybook is set up) - Run tests:
npm test
4. Build for Production
npm run build-cro-componentsYour components will be built to ./cro-component-exports/
5. Use in CRO Tests
// Import your built components
import './cro-component-exports/MyButton.js';
import './cro-component-exports/ContactForm.js';
// Use them in your CRO tests
const button = document.createElement('cro-my-button');
button.setAttribute('label', 'Special Offer');
document.body.appendChild(button);Build and Export
The build process automatically:
- Discovers Components: Scans your
cro-components/directory recursively - Bundles with Rollup: Creates optimized ES modules in
./cro-component-exports/ - Generates Exports: Updates your
package.jsonwith proper export paths - Creates Type Definitions: Generates TypeScript definitions for better IDE support
Export Structure
After building, components are available as:
// Your custom components
import './cro-component-exports/MyButton.js';
import './cro-component-exports/ContactForm.js';
import './cro-component-exports/SignupModal.js';
// Core components (if you've imported them)
import './cro-component-exports/Button.js';
import './cro-component-exports/Header.js';
import './cro-component-exports/Overlay.js';Testing and Linting
Running Tests
npm test # Run all tests
npm test -- --watch # Run tests in watch mode
npm test MyButton.test.js # Run specific test fileCode Quality (if ESLint/Prettier are set up in your project)
npm run lint:check # Check for linting errors
npm run lint:fix # Auto-fix linting errors
npm run prettier:check # Check formatting
npm run prettier:fix # Auto-fix formattingProject Structure
your-project/
├── cro-components/ # Your component source files
│ ├── cro-my-button/
│ │ ├── MyButton.js
│ │ ├── MyButton.stories.js
│ │ └── MyButton.test.js
│ ├── forms/
│ │ └── cro-contact-form/
│ │ ├── ContactForm.js
│ │ ├── ContactForm.stories.js
│ │ └── ContactForm.test.js
│ └── README.md
├── cro-component-exports/ # Built components (generated)
│ ├── MyButton.js
│ ├── ContactForm.js
│ └── index.d.ts
├── node_modules/
│ └── cro-components/ # This package
├── package.json # Auto-updated with exports
└── .gitignore # Auto-updatedAdvanced Usage
Component Styling Best Practices
- Use Shadow DOM: All components use Shadow DOM for style encapsulation
- CSS Custom Properties: Use CSS variables for theming:
style.textContent = `
.my-component {
background: var(--component-bg, #ffffff);
color: var(--component-color, #333333);
padding: var(--component-padding, 16px);
}
`;- Responsive Design: Ensure components work across different screen sizes
- Accessibility: Include proper ARIA attributes and keyboard navigation
Integration with CRO Tools
Optimizely Integration
import './cro-component-exports/MyButton.js';
const button = document.createElement('cro-my-button');
button.setAttribute('label', 'Get Started');
// Add event tracking
button.addEventListener('click', () => {
window.optimizely = window.optimizely || [];
window.optimizely.push({
type: 'event',
eventName: 'cro_button_click',
tags: {
component: 'cro-my-button',
label: 'Get Started'
}
});
});
document.querySelector('.target-container').appendChild(button);Google Optimize Integration
import './cro-component-exports/MyOverlay.js';
const overlay = document.createElement('cro-my-overlay');
overlay.setAttribute('header', 'Special Offer!');
// Track interactions
overlay.addEventListener('click', (e) => {
if (e.target.classList.contains('close-button')) {
gtag('event', 'overlay_close', {
event_category: 'CRO',
event_label: 'Special Offer Overlay'
});
}
});Troubleshooting
Common Issues
Components not building
- Ensure you've run
npx cro-setupornode node_modules/cro-components/postinstall.js - Check that
cro-components/directory exists - Verify component files end with
.jsand not.stories.jsor.test.js
- Ensure you've run
Build script not found
- Run the manual setup:
npx cro-setup - Or add manually to package.json:
"build-cro-components": "npx cro-build"
- Run the manual setup:
Components not appearing in exports
- Check file naming convention
- Ensure files are in the correct directory structure
- Run
npm run build-cro-componentsto rebuild
Debug Commands
# Check what components are being discovered
ls -la cro-components/
# Validate component syntax
node -c cro-components/cro-my-button/MyButton.js
# Check if build script exists
cat package.json | grep build-cro-componentsDependencies
Key Dependencies
- Rollup: JavaScript bundler for creating optimized ES modules
- Node.js: Runtime for the build tools
- npm: Package manager
Optional Dependencies
- Storybook: Component development and documentation environment
- Jest: Testing framework
- ESLint: Code linting
- Prettier: Code formatting
License
This project is licensed under the ISC License - see the LICENSE file for details.
Support
For questions, issues, or contributions:
- Check the GitHub Issues page
- Review the component documentation in your
cro-components/README.md - Test components using the build and export workflow
Made for better conversion optimization 🚀
