mirror of
https://github.com/sanjeed5/awesome-cursor-rules-mdc.git
synced 2026-11-30 06:42:54 +08:00
Add comprehensive MDC files for various technologies and frameworks
Generate a large set of Markdown Configuration (MDC) files covering best practices across multiple domains, including: - AI/ML libraries (PyTorch, TensorFlow, scikit-learn) - Backend frameworks (Django, FastAPI, Express, Spring Boot) - Frontend frameworks (React, Vue, Angular) - Cloud platforms (AWS, Azure, GCP) - Development tools (Docker, Kubernetes, Terraform) - State management libraries - UI component libraries - Web technologies and APIs Enhance project documentation with detailed, standardized best practice guidelines for different technologies and frameworks
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
---
|
||||
description: Comprehensive guide for AWS Lambda development, covering best practices for code organization, performance, security, testing, and common pitfalls. Focuses on building robust, scalable, and secure serverless applications using AWS Lambda.
|
||||
globs: **/*.{js,ts,jsx,tsx,py,java,go,c#,cs,ps1,sh}
|
||||
---
|
||||
- **Use Documented APIs Only:** Rely solely on AWS Lambda's documented APIs to ensure compatibility and avoid unexpected behavior. Do not use undocumented or internal APIs.
|
||||
|
||||
- **Single Responsibility Principle:** Design Lambda functions to perform a single, well-defined task. This promotes modularity, testability, and maintainability.
|
||||
|
||||
- **Native AWS Integrations:** Prefer native AWS integrations (e.g., SQS, SNS, Kinesis) for passing data between services instead of custom code, which enhances scalability and reduces complexity.
|
||||
|
||||
- **Local Dependencies:** Store and reference dependencies locally within the Lambda function package to minimize latency and improve cold start times.
|
||||
|
||||
- **Limit Variable Re-initialization:** Initialize variables outside the function handler to leverage execution context reuse and reduce initialization overhead.
|
||||
|
||||
- **Thorough Testing:** Implement a comprehensive testing strategy including unit, integration, and end-to-end tests to ensure function correctness and reliability.
|
||||
|
||||
## 1. Code Organization and Structure:
|
||||
|
||||
- **Directory Structure:**
|
||||
- Organize code into logical directories based on functionality (e.g., `src`, `tests`, `utils`).
|
||||
- Separate handler logic from core business logic.
|
||||
- Example:
|
||||
|
||||
my-lambda-function/
|
||||
├── src/
|
||||
│ ├── handler.js # Lambda handler function
|
||||
│ ├── business-logic.js # Core business logic
|
||||
│ └── utils.js # Utility functions
|
||||
├── tests/
|
||||
│ ├── handler.test.js # Unit tests for handler.js
|
||||
│ └── ...
|
||||
├── package.json # Dependencies and scripts
|
||||
└── ...
|
||||
|
||||
|
||||
- **File Naming Conventions:**
|
||||
- Use descriptive and consistent file names.
|
||||
- Follow a naming convention (e.g., `kebab-case`, `PascalCase`) consistently.
|
||||
- Example: `user-service.js`, `UserService.java`.
|
||||
|
||||
- **Module Organization:**
|
||||
- Break down large functions into smaller, reusable modules.
|
||||
- Use appropriate module systems (e.g., ES modules in JavaScript, packages in Python, modules in Go).
|
||||
- Example (JavaScript):
|
||||
javascript
|
||||
// src/user-service.js
|
||||
export function createUser(userData) { ... }
|
||||
export function getUser(userId) { ... }
|
||||
|
||||
|
||||
- **Component Architecture:**
|
||||
- Design Lambda functions as part of a larger component-based architecture.
|
||||
- Decouple functions from specific triggers (e.g., API Gateway, SQS) to increase reusability.
|
||||
- Implement interfaces or contracts to define interactions between components.
|
||||
|
||||
- **Code Splitting Strategies:**
|
||||
- Use code splitting techniques to reduce Lambda function package size.
|
||||
- Implement dynamic imports or lazy loading for infrequently used modules (where supported by the runtime).
|
||||
- Leverage layers for shared dependencies across multiple functions.
|
||||
|
||||
## 2. Common Patterns and Anti-patterns:
|
||||
|
||||
- **Design Patterns:**
|
||||
- **Event-Driven Architecture:** Design functions to respond to events from various AWS services.
|
||||
- **Command Pattern:** Encapsulate requests as objects to decouple request processing from the handler.
|
||||
- **Dependency Injection:** Inject dependencies into functions to improve testability and maintainability.
|
||||
|
||||
- **Recommended Approaches:**
|
||||
- **Configuration Management:** Use environment variables or AWS Systems Manager Parameter Store for configuration data.
|
||||
- **Logging:** Implement structured logging with timestamps, request IDs, and relevant context.
|
||||
- **Idempotency:** Ensure functions are idempotent to handle potential retry scenarios gracefully.
|
||||
|
||||
- **Anti-patterns:**
|
||||
- **Monolithic Functions:** Avoid creating large, complex functions that perform multiple unrelated tasks.
|
||||
- **Hardcoding Secrets:** Never hardcode sensitive information (e.g., API keys, passwords) in code. Use AWS Secrets Manager or environment variables.
|
||||
- **Excessive Dependencies:** Minimize the number of dependencies to reduce package size and improve cold start times.
|
||||
|
||||
- **State Management:**
|
||||
- Use external services (e.g., DynamoDB, Redis) for persistent state management.
|
||||
- Leverage Lambda layers for caching frequently accessed data.
|
||||
- Design functions to be stateless whenever possible.
|
||||
|
||||
- **Error Handling:**
|
||||
- Implement robust error handling mechanisms, including try-catch blocks and error logging.
|
||||
- Use custom exceptions for specific error scenarios.
|
||||
- Return meaningful error messages to the caller.
|
||||
- Implement dead-letter queues (DLQs) for asynchronous invocations to handle failed events.
|
||||
|
||||
## 3. Performance Considerations:
|
||||
|
||||
- **Optimization Techniques:**
|
||||
- **Minimize Package Size:** Reduce the size of the deployment package by removing unnecessary files and dependencies.
|
||||
- **Optimize Dependencies:** Use lightweight dependencies and avoid importing entire libraries when only specific functions are needed.
|
||||
- **Code Optimization:** Profile and optimize code for performance bottlenecks.
|
||||
- **Connection Reuse:** Reuse database connections, HTTP clients, and other resources to minimize overhead.
|
||||
|
||||
- **Memory Management:**
|
||||
- Allocate sufficient memory to Lambda functions based on their needs.
|
||||
- Monitor memory usage and adjust the memory allocation accordingly.
|
||||
- Clean up resources (e.g., closing file streams, releasing memory) to avoid memory leaks.
|
||||
|
||||
- **Cold Starts:**
|
||||
- Minimize cold start times by reducing package size, optimizing dependencies, and using provisioned concurrency.
|
||||
- Use initilization logic outside the handler to take advantage of container reuse
|
||||
|
||||
- **Bundle Size Optimization:**
|
||||
- Use tools like Webpack or esbuild to bundle and optimize code for production.
|
||||
- Minify code and remove unused code (dead code elimination).
|
||||
|
||||
- **Lazy Loading Strategies:**
|
||||
- Implement lazy loading or dynamic imports for infrequently used modules to reduce initial load time.
|
||||
|
||||
## 4. Security Best Practices:
|
||||
|
||||
- **Common Vulnerabilities:**
|
||||
- **Injection Attacks:** Prevent SQL injection, command injection, and other injection attacks by validating and sanitizing input.
|
||||
- **Cross-Site Scripting (XSS):** Protect against XSS vulnerabilities by encoding output properly.
|
||||
- **Insecure Deserialization:** Avoid deserializing untrusted data to prevent arbitrary code execution.
|
||||
- **Broken Authentication:** Implement strong authentication and authorization mechanisms.
|
||||
|
||||
- **Input Validation:**
|
||||
- Validate all input data to prevent malicious or invalid data from being processed.
|
||||
- Use schema validation libraries to enforce data types and formats.
|
||||
- Sanitize input data to remove potentially harmful characters or code.
|
||||
|
||||
- **Authentication and Authorization:**
|
||||
- Use AWS Identity and Access Management (IAM) roles to grant Lambda functions the necessary permissions.
|
||||
- Implement authentication and authorization mechanisms to protect sensitive resources.
|
||||
- Use AWS Cognito for user authentication and authorization.
|
||||
|
||||
- **Data Protection:**
|
||||
- Encrypt sensitive data at rest and in transit.
|
||||
- Use AWS Key Management Service (KMS) to manage encryption keys.
|
||||
- Mask or redact sensitive data in logs.
|
||||
|
||||
- **Secure API Communication:**
|
||||
- Use HTTPS for all API communication.
|
||||
- Implement API authentication and authorization using API keys, JWT tokens, or other secure mechanisms.
|
||||
- Protect against common web application attacks (e.g., CSRF, DDoS).
|
||||
|
||||
## 5. Testing Approaches:
|
||||
|
||||
- **Unit Testing:**
|
||||
- Write unit tests to verify the correctness of individual functions or modules.
|
||||
- Use mocking or stubbing to isolate units of code from external dependencies.
|
||||
- Use testing frameworks (e.g., Jest, Mocha, pytest) to automate unit testing.
|
||||
|
||||
- **Integration Testing:**
|
||||
- Write integration tests to verify the interactions between different components or services.
|
||||
- Test the integration of Lambda functions with other AWS services (e.g., SQS, DynamoDB).
|
||||
- Use tools like AWS SAM Local to run integration tests locally.
|
||||
|
||||
- **End-to-End Testing:**
|
||||
- Write end-to-end tests to verify the entire application flow from end to end.
|
||||
- Simulate user interactions to test the application's functionality.
|
||||
- Use tools like Selenium or Cypress to automate end-to-end testing.
|
||||
|
||||
- **Test Organization:**
|
||||
- Organize tests into separate directories based on the type of test (e.g., unit, integration, e2e).
|
||||
- Use descriptive test names to clearly identify the purpose of each test.
|
||||
- Follow a consistent testing strategy across all projects.
|
||||
|
||||
- **Mocking and Stubbing:**
|
||||
- Use mocking or stubbing to replace external dependencies with controlled test doubles.
|
||||
- Mock AWS SDK calls to isolate Lambda functions from AWS services.
|
||||
- Use mocking frameworks (e.g., Jest, Sinon.js) to simplify mocking and stubbing.
|
||||
|
||||
## 6. Common Pitfalls and Gotchas:
|
||||
|
||||
- **Frequent Mistakes:**
|
||||
- **Incorrect IAM Permissions:** Ensure that Lambda functions have the necessary IAM permissions to access required AWS resources.
|
||||
- **Timeout Errors:** Increase the Lambda function timeout if it takes longer than expected to complete.
|
||||
- **Memory Exhaustion:** Allocate sufficient memory to Lambda functions to avoid memory exhaustion errors.
|
||||
- **Unclosed Connections:** Close database connections, HTTP clients, and other resources properly to avoid resource leaks.
|
||||
|
||||
- **Edge Cases:**
|
||||
- **Concurrent Invocations:** Handle concurrent invocations gracefully to avoid race conditions or data corruption.
|
||||
- **Throttling:** Implement retry mechanisms to handle throttling errors from AWS services.
|
||||
- **Error Retries:** Design functions to be idempotent to handle potential retry scenarios gracefully.
|
||||
|
||||
- **Version-Specific Issues:**
|
||||
- Be aware of version-specific issues or compatibility concerns when upgrading Lambda function runtimes or dependencies.
|
||||
- Test Lambda functions thoroughly after upgrading to ensure they still function correctly.
|
||||
|
||||
- **Compatibility Concerns:**
|
||||
- Ensure that Lambda functions are compatible with the supported runtime environment.
|
||||
- Use compatible versions of dependencies to avoid compatibility issues.
|
||||
|
||||
- **Debugging Strategies:**
|
||||
- Use CloudWatch Logs to debug Lambda function execution.
|
||||
- Use AWS X-Ray to trace and analyze Lambda function performance.
|
||||
- Use local debugging tools (e.g., AWS SAM Local) to debug Lambda functions locally.
|
||||
|
||||
## 7. Tooling and Environment:
|
||||
|
||||
- **Recommended Tools:**
|
||||
- **AWS SAM:** Use AWS SAM (Serverless Application Model) to define and deploy Lambda functions and other serverless resources.
|
||||
- **AWS CLI:** Use the AWS CLI (Command Line Interface) to manage AWS resources from the command line.
|
||||
- **Terraform/CloudFormation:** Use Infrastructure as Code (IaC) tools like Terraform or CloudFormation to provision and manage AWS infrastructure.
|
||||
- **IDE:** Use an IDE with support for Lambda development (e.g., VS Code with the AWS Toolkit extension).
|
||||
|
||||
- **Build Configuration:**
|
||||
- Use build tools (e.g., npm, yarn, Maven, Gradle) to manage dependencies and build Lambda function packages.
|
||||
- Configure build scripts to automate common tasks (e.g., linting, testing, bundling).
|
||||
|
||||
- **Linting and Formatting:**
|
||||
- Use linters (e.g., ESLint, Pylint) to enforce coding standards and identify potential code quality issues.
|
||||
- Use formatters (e.g., Prettier, Black) to automatically format code consistently.
|
||||
|
||||
- **Deployment:**
|
||||
- Use AWS SAM CLI or other deployment tools to deploy Lambda functions to AWS.
|
||||
- Implement blue/green deployments or canary deployments to minimize downtime during deployments.
|
||||
- Automate deployments using CI/CD pipelines.
|
||||
|
||||
- **CI/CD Integration:**
|
||||
- Integrate Lambda function deployments with CI/CD pipelines to automate testing and deployment.
|
||||
- Use CI/CD tools (e.g., Jenkins, CircleCI, GitHub Actions) to build, test, and deploy Lambda functions.
|
||||
|
||||
@file aws-lambda-security.mdc
|
||||
@file aws-lambda-performance.mdc
|
||||
@@ -0,0 +1,210 @@
|
||||
---
|
||||
description: This rule outlines best practices for developing and maintaining modal libraries, covering aspects from code organization and performance to security and testing. It aims to guide developers in creating robust, efficient, and user-friendly modal components.
|
||||
globs: **/*.{js,jsx,ts,tsx,mjs,cjs}
|
||||
---
|
||||
- **Library Information:**
|
||||
- Name: modal
|
||||
- Category: cloud_platforms
|
||||
- Subcategory: serverless
|
||||
|
||||
- **General Principles:**
|
||||
- **Clarity and Purpose:** Modals should have a clear and concise purpose, conveyed through a descriptive title. Content should be helpful and action-oriented.
|
||||
- **User Control:** Prioritize user control by using modals sparingly and providing visible, intuitive close options (e.g., close button, overlay click).
|
||||
- **Accessibility:** Ensure modals are accessible to all users by testing with assistive technologies and adhering to accessibility standards (e.g., ARIA attributes).
|
||||
|
||||
- **Code Organization and Structure:**
|
||||
- **Directory Structure:**
|
||||
- `src/`: Contains the source code of the modal library.
|
||||
- `components/`: Reusable modal components (e.g., `Modal.jsx`, `ModalHeader.jsx`, `ModalBody.jsx`, `ModalFooter.jsx`).
|
||||
- `hooks/`: Custom hooks for managing modal state and logic (e.g., `useModal.js`).
|
||||
- `utils/`: Utility functions (e.g., validation, formatting).
|
||||
- `styles/`: CSS or styling components.
|
||||
- `types/`: TypeScript type definitions.
|
||||
- `examples/`: Example usage of the modal library.
|
||||
- `tests/`: Unit and integration tests.
|
||||
- `docs/`: Documentation for the library.
|
||||
- **File Naming Conventions:**
|
||||
- Use descriptive names for components (e.g., `ConfirmationModal.jsx`, `InputModal.jsx`).
|
||||
- Follow a consistent naming convention (e.g., PascalCase for components, camelCase for variables and functions).
|
||||
- **Module Organization:**
|
||||
- Organize code into logical modules and submodules. Each module should have a specific responsibility.
|
||||
- Use ES modules (import/export) for modularity and tree-shaking.
|
||||
- **Component Architecture:**
|
||||
- Favor a component-based architecture. Each modal element (header, body, footer) should be a reusable component.
|
||||
- Use composition to create complex modals from smaller, reusable components.
|
||||
- **Code Splitting Strategies:**
|
||||
- Consider code splitting for large modal libraries to improve initial load time.
|
||||
- Lazy load modal content or components that are not immediately needed.
|
||||
|
||||
- **Common Patterns and Anti-patterns:**
|
||||
- **Design Patterns:**
|
||||
- **Controlled Components:** Use controlled components for modal content to manage state effectively.
|
||||
- **Render Props or Hooks:** Provide flexibility in rendering modal content using render props or custom hooks.
|
||||
- **Provider Pattern:** Use a context provider to manage global modal state and configuration.
|
||||
- **Recommended Approaches:**
|
||||
- Use a state management library (e.g., Zustand, Jotai) for complex modal state.
|
||||
- Implement a modal queue to manage multiple modals efficiently.
|
||||
- Provide a consistent API for opening and closing modals.
|
||||
- **Anti-patterns:**
|
||||
- **Overusing Modals:** Avoid using modals for non-critical information or actions.
|
||||
- **Deeply Nested Components:** Minimize deeply nested components within modals to improve performance.
|
||||
- **Ignoring Accessibility:** Neglecting accessibility can make the modal unusable for some users.
|
||||
- **State Management Best Practices:**
|
||||
- Choose a state management solution that fits the complexity of the modal library.
|
||||
- Keep state minimal and derive values when possible.
|
||||
- Use immutable data structures to prevent unexpected side effects.
|
||||
- **Error Handling Patterns:**
|
||||
- Implement error boundaries to catch errors within modals.
|
||||
- Provide informative error messages to the user.
|
||||
- Log errors for debugging purposes.
|
||||
|
||||
- **Performance Considerations:**
|
||||
- **Optimization Techniques:**
|
||||
- Memoize components to prevent unnecessary re-renders.
|
||||
- Use virtualization for long lists of modal content.
|
||||
- Debounce or throttle event handlers to improve responsiveness.
|
||||
- **Memory Management:**
|
||||
- Clean up event listeners and timers when the modal is closed.
|
||||
- Avoid creating large data structures within modals.
|
||||
- Release resources when the modal is unmounted.
|
||||
- **Rendering Optimization:**
|
||||
- Use CSS transforms for animations instead of modifying layout properties.
|
||||
- Minimize the number of DOM updates.
|
||||
- Use techniques like `requestAnimationFrame` for smoother animations.
|
||||
- **Bundle Size Optimization:**
|
||||
- Use tree-shaking to remove unused code from the bundle.
|
||||
- Minify and compress the code.
|
||||
- Use code splitting to reduce the initial bundle size.
|
||||
- **Lazy Loading Strategies:**
|
||||
- Lazy load modal content or components that are not immediately visible.
|
||||
- Use dynamic imports to load modules on demand.
|
||||
|
||||
- **Security Best Practices:**
|
||||
- **Common Vulnerabilities:**
|
||||
- **Cross-Site Scripting (XSS):** Sanitize user inputs to prevent XSS attacks.
|
||||
- **Injection Attacks:** Protect against injection attacks by validating user inputs.
|
||||
- **Input Validation:**
|
||||
- Validate all user inputs within the modal.
|
||||
- Use appropriate data types and formats.
|
||||
- Sanitize inputs to remove malicious code.
|
||||
- **Authentication and Authorization Patterns:**
|
||||
- Implement authentication and authorization mechanisms to protect sensitive data.
|
||||
- Use secure cookies or tokens to store user credentials.
|
||||
- Enforce access control policies.
|
||||
- **Data Protection Strategies:**
|
||||
- Encrypt sensitive data at rest and in transit.
|
||||
- Use secure storage mechanisms to protect data.
|
||||
- Implement data loss prevention measures.
|
||||
- **Secure API Communication:**
|
||||
- Use HTTPS for all API communication.
|
||||
- Validate API responses.
|
||||
- Protect API keys and secrets.
|
||||
|
||||
- **Testing Approaches:**
|
||||
- **Unit Testing Strategies:**
|
||||
- Test individual components and functions in isolation.
|
||||
- Use mocking and stubbing to isolate dependencies.
|
||||
- Test edge cases and error conditions.
|
||||
- **Integration Testing:**
|
||||
- Test the interaction between components and modules.
|
||||
- Verify that the modal works correctly with other parts of the application.
|
||||
- **End-to-End Testing:**
|
||||
- Test the entire modal workflow from start to finish.
|
||||
- Use automated testing tools to simulate user interactions.
|
||||
- **Test Organization:**
|
||||
- Organize tests into logical groups based on functionality.
|
||||
- Use descriptive test names.
|
||||
- Keep tests concise and focused.
|
||||
- **Mocking and Stubbing:**
|
||||
- Use mocking to simulate external dependencies.
|
||||
- Use stubbing to replace complex functions with simpler versions.
|
||||
- Avoid over-mocking, as it can lead to brittle tests.
|
||||
|
||||
- **Common Pitfalls and Gotchas:**
|
||||
- **Frequent Mistakes:**
|
||||
- Forgetting to handle focus management when opening/closing modal
|
||||
- Not properly managing the stacking context of modals.
|
||||
- Ignoring keyboard navigation.
|
||||
- **Edge Cases:**
|
||||
- Handling modals within modals.
|
||||
- Supporting different screen sizes and orientations.
|
||||
- Handling modals in server-side rendered applications.
|
||||
- **Version-Specific Issues:**
|
||||
- Be aware of compatibility issues with different versions of dependencies.
|
||||
- Test the modal library with different browsers and devices.
|
||||
- **Compatibility Concerns:**
|
||||
- Ensure the modal library is compatible with different JavaScript frameworks and libraries.
|
||||
- Test with different browsers and devices.
|
||||
- **Debugging Strategies:**
|
||||
- Use browser developer tools to inspect the modal's HTML and CSS.
|
||||
- Use logging to track the flow of execution.
|
||||
- Use a debugger to step through the code.
|
||||
|
||||
- **Tooling and Environment:**
|
||||
- **Recommended Development Tools:**
|
||||
- VS Code with extensions for React/TypeScript/ESLint.
|
||||
- Chrome DevTools for debugging.
|
||||
- **Build Configuration:**
|
||||
- Use a build tool like webpack, Parcel, or Rollup.
|
||||
- Configure the build tool to optimize the bundle size.
|
||||
- **Linting and Formatting:**
|
||||
- Use ESLint to enforce code style and detect potential errors.
|
||||
- Use Prettier to automatically format the code.
|
||||
- **Deployment Best Practices:**
|
||||
- Deploy the modal library to a CDN for fast and reliable delivery.
|
||||
- Use version control to track changes to the code.
|
||||
- Automate the deployment process.
|
||||
- **CI/CD Integration:**
|
||||
- Integrate the modal library with a CI/CD pipeline for automated testing and deployment.
|
||||
- Use a CI/CD tool like Jenkins, Travis CI, or CircleCI.
|
||||
|
||||
- **Accessibility Considerations (WCAG Compliance):**
|
||||
- **Keyboard Navigation:** Ensure all interactive elements within the modal are reachable and operable using the keyboard. Use tab index appropriately.
|
||||
- **Focus Management:** Trap focus within the modal when it's open. Return focus to the previously focused element when the modal is closed. Prevent focus leaking to elements behind the modal.
|
||||
- **ARIA Attributes:**
|
||||
- Use `role="dialog"` or `role="alertdialog"` to indicate the modal's purpose.
|
||||
- Use `aria-labelledby` to associate the modal's title with the modal element.
|
||||
- Use `aria-describedby` to associate the modal's content with the modal element.
|
||||
- Implement `aria-modal="true"` to signify the modal is blocking interaction with the rest of the page.
|
||||
- **Color Contrast:** Ensure sufficient color contrast between text and background elements.
|
||||
- **Screen Reader Compatibility:** Test the modal with screen readers to ensure content is read correctly and controls are accessible.
|
||||
- **Visual Cues:** Provide clear visual cues to indicate the active element.
|
||||
- **Alternative Text:** Ensure images have descriptive alternative text.
|
||||
|
||||
- **UI/UX Best Practices:**
|
||||
- **Modal Size and Positioning:** Ensure the modal is appropriately sized and positioned on the screen. Avoid modals that are too large or obstruct important content.
|
||||
- **Animation and Transitions:** Use subtle animations and transitions to enhance the user experience. Avoid excessive or distracting animations.
|
||||
- **Responsiveness:** Ensure the modal is responsive and adapts to different screen sizes and orientations.
|
||||
- **Dark Overlay:** Use a dark overlay to dim the background and focus the user's attention on the modal. The overlay should be semi-transparent.
|
||||
- **Clear Calls to Action:** Use clear and concise button labels for calls to action. Avoid ambiguous or confusing labels.
|
||||
|
||||
- **Example Snippets (Illustrative):**
|
||||
- **React Component:**
|
||||
jsx
|
||||
import React, { useState } from 'react';
|
||||
|
||||
const Modal = ({ isOpen, onClose, children }) => {
|
||||
if (!isOpen) return null;
|
||||
|
||||
return (
|
||||
|
||||
|
||||
{children}
|
||||
<button onClick={onClose}>Close</button>
|
||||
|
||||
|
||||
);
|
||||
};
|
||||
|
||||
export default Modal;
|
||||
|
||||
- **TypeScript Type Definitions:**
|
||||
typescript
|
||||
interface ModalProps {
|
||||
isOpen: boolean;
|
||||
onClose: () => void;
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
|
||||
By adhering to these best practices, you can create robust, accessible, and user-friendly modal libraries that enhance the overall user experience of your applications.
|
||||
Reference in New Issue
Block a user