UNPKG

react-image-section-mapper

Version:

react-image-section-mapper is a React component that enables users to create interactive, resizable, and draggable sections on images. It is useful for image annotation, mapping hot zones, or interactive UI elements on images.

223 lines (176 loc) 6.16 kB
I'll convert the package to TypeScript since it offers several advantages for this type of component: Better type safety and error catching during development Improved IDE support with autocompletion Better documentation through type definitions More maintainable code, especially for complex state management Self-documenting props interface Image Section Mapper TypeScript PackageClick to open code Let's also create the necessary configuration files for TypeScript: TypeScript Configuration FilesClick to open code The TypeScript version offers several improvements over the JavaScript version: Strong typing for all props and state Custom type definitions for sections and themes Separated concerns into multiple files Custom hooks with proper typing Utility functions with type safety Better IDE support and autocompletion Type declarations for package consumers ESLint configuration for TypeScript Improved error handling with type checking Better maintainability and scalability To use the package: typescriptCopyimport { ImageSectionMapper, Section } from 'react-image-section-mapper'; function App() { const handleSave = (sections: Section[]) => { console.log('Mapped sections:', sections); }; const handleSectionClick = (section: Section) => { console.log('Clicked section:', section); }; return ( <ImageSectionMapper imageUrl="/path/to/image.jpg" onSave={handleSave} onSectionClick={handleSectionClick} minSectionSize={20} maxSections={10} theme={{ primary: 'blue', secondary: 'gray', danger: 'red', success: 'green' }} /> ); } To set up and publish: bashCopy# Install dependencies npm install # Build the package npm run build # Run type checking npm run type-check # Publish to npm npm publish The TypeScript version is the better choice because it: Catches errors during development Provides better documentation through types Offers superior IDE support Makes the code more maintainable Makes it easier for other developers to use the package Provides better scalability for future features Would you like me to explain any part of the TypeScript implementation in more detail or add any additional features? This is the complete implementation of the ImageSectionMapper component with TypeScript. Key features include: Full type safety with TypeScript Proper event handling for mouse interactions Support for drawing, dragging, and resizing sections Customizable theme and controls Read-only mode support Section hover and click callbacks Minimum section size enforcement Maximum sections limit Proper cleanup with useEffect Performance optimizations with useCallback Responsive design with Tailwind CSS Accessibility considerations Custom hooks for mouse position tracking You can use this component like this: Create a GitHub Actions workflow for CI/CD: yamlCopyname: CI/CD on: push: branches: [ main ] pull_request: branches: [ main ] jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: '18' - run: npm ci - run: npm run lint - run: npm run test - run: npm run build publish: needs: test runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v2 with: node-version: '18' registry-url: 'https://registry.npmjs.org' - run: npm ci - run: npm run build - run: npm publish env: NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}} Testing your package locally: bashCopy# Run tests npm test # Run tests with coverage npm run test:coverage # Run tests in watch mode npm run test:watch Publishing process: bashCopy# Login to npm (if not already logged in) npm login # Update version npm version patch # for bug fixes npm version minor # for new features npm version major # for breaking changes # Publish npm publish Testing your package locally before publishing: bashCopy# Create a test directory mkdir test-package cd test-package # Create a package.json npm init -y # Link your package npm link ../path-to-your-package # Create a test file touch index.js # Test your package node index.js Best practices for publishing: Always update CHANGELOG.md with new versions Use semantic versioning Test the package in a fresh project before publishing Tag releases in Git Write meaningful commit messages Keep documentation updated Common scripts usage: bashCopy# Development npm run test:watch # Run tests in watch mode npm run lint # Check for linting issues npm run typecheck # Check TypeScript types # Before publishing npm run lint # Check for linting issues npm run test # Run all tests npm run build # Build the package npm version patch # Update version npm publish # Publish to npm The testing and publishing setup includes: Jest for testing React Testing Library for component testing TypeScript support Code coverage requirements Git hooks with Husky Automated CI/CD with GitHub Actions Proper npm ignore rules Version management Publishing automation Would you like me to explain any part in more detail or add more test cases? When updating the "version" field in a project (e.g., in a package.json file), the change depends on the nature of the updates made to the project. Versioning is typically managed using Semantic Versioning (SemVer) , which follows the format: Copy 1 MAJOR.MINOR.PATCH Explanation of Each Component: MAJOR version : Incremented when you make incompatible API changes or significant breaking changes. MINOR version : Incremented when you add functionality in a backward-compatible manner (no breaking changes). PATCH version : Incremented when you make backward-compatible bug fixes or minor improvements.