Responsive Adaptation
React Widget has built-in complete responsive adaptation solution, components automatically adjust layout and styles based on screen size.
Breakpoint Definitions
| Breakpoint | Width Range | Description |
|---|---|---|
| mobile | < 768px | Mobile devices |
| tablet | 768px - 1024px | Tablet devices |
| desktop | > 1024px | Desktop devices |
variant Attribute
All responsive-enabled components provide variant attribute:
type Variant = 'auto' | 'mobile' | 'desktop';
| Value | Description |
|---|---|
'auto' | Automatically detect screen size, dynamically switch (default value) |
'mobile' | Force mobile styles |
'desktop' | Force desktop styles |
Usage Example
// Auto adaptation (default)
<ControlPanel variant="auto" />
// Force mobile styles
<ControlPanel variant="mobile" />
// Force desktop styles
<ConversationList variant="desktop" />
Application Scenarios
- Embedded scenario: Force use
mobilemode when embedding in small size area on PC page - Debug scenario: Test specific platform style behavior
- Fixed layout: Don't want component to change with screen
Responsive Hooks
Widget exports responsive detection Hooks:
import {
useIsMobile,
useIsTablet,
useIsDesktop,
useResponsive
} from '@bdky/aaas-pilot-kit-react-widget';
useIsMobile
Detect if mobile:
function MyComponent() {
const isMobile = useIsMobile();
return (
<div>
{isMobile ? 'Mobile View' : 'Desktop View'}
</div>
);
}
useIsTablet
Detect if tablet:
function MyComponent() {
const isTablet = useIsTablet();
if (isTablet) {
return <TabletLayout />;
}
// ...
}
useIsDesktop
Detect if desktop:
function MyComponent() {
const isDesktop = useIsDesktop();
return (
<div style={{width: isDesktop ? 800 : '100%'}}>
Content
</div>
);
}
useResponsive
Get complete responsive information:
function MyComponent() {
const {isMobile, isTablet, isDesktop} = useResponsive();
return (
<div className={
isMobile ? 'layout-mobile' :
isTablet ? 'layout-tablet' :
'layout-desktop'
}>
Content
</div>
);
}
CSS Class Name Convention
Components automatically add corresponding CSS class names based on current mode:
| Class Name Suffix | Description |
|---|---|
--mobile | Mobile mode |
--desktop | Desktop mode |
/* Example: Customize mobile styles */
.apk-control-panel--mobile {
padding: 10px;
}
/* Example: Customize desktop styles */
.apk-control-panel--desktop {
padding: 20px;
}
Component Differences Quick Reference
ConversationList
| Feature | PC | Mobile |
|---|---|---|
| Conversation display | Show all history | Show only latest one |
| Close button | Show | Hide |
| Scroll follow button | Show | Hide |
| Position | Absolute positioning left side | Relative positioning |
ControlPanel
| Feature | PC | Mobile |
|---|---|---|
| Input box width | 479px fixed | 100% adaptive |
| Button size | 54-55px | 50px |
| Layout | Centered absolute positioning | Full-width flex |
| Hangup button | Optional display | Always display |
Input
| Feature | PC | Mobile |
|---|---|---|
| Width | 479px | 100% |
| Height | 55px | 50px |
| Border radius | 28px | 20px |
| Padding | 24px | 16px |
RecommendedQuestions
| Feature | PC | Mobile |
|---|---|---|
| Layout | Horizontal arrangement | Vertical arrangement |
| Background | Semi-transparent capsule | Transparent |
| Question item | Text + arrow | Card style |
| Title | Show | Default hidden |
| Separator | Show | Hide |
StatusOverlay
| Feature | PC | Mobile |
|---|---|---|
| Content alignment | Vertically centered | Top aligned + padding |
| Avatar size | 120px | 100px |
| Font size | 16px | 14px |
Conversation
| Feature | PC | Mobile |
|---|---|---|
| Border radius | 20px | Different corners different radius |
| Margin | Left/right 30px | Left/right 39px |
| Background | Semi-transparent | Deeper semi-transparent |
Best Practices
1. Prioritize auto Mode
Unless there are special requirements, it's recommended to use default auto mode to let components automatically adapt:
// Recommended
<ControlPanel />
// Only specify when necessary
<ControlPanel variant="mobile" />
2. Conditional Rendering Optimization
For completely different layouts, use conditional rendering instead of style switching:
const isMobile = useIsMobile();
// Recommended: Completely different component structures
{isMobile ? (
<MobileLayout>
<ConversationList />
<ControlPanel />
</MobileLayout>
) : (
<DesktopLayout>
<Sidebar>
<ConversationList />
</Sidebar>
<Main>
<Subtitle />
<ControlPanel />
</Main>
</DesktopLayout>
)}
3. Combine variant
Keep parent-child component's variant consistent:
const variant = 'mobile'; // Or get from props/state
<Layout direction="vertical">
<ConversationList variant={variant} />
<ControlPanel variant={variant} />
</Layout>
4. Custom Responsive Styles
Use CSS class name hooks to add custom responsive styles:
/* Base styles */
.apk-control-panel {
transition: all 0.3s ease;
}
/* Mobile special styles */
.apk-control-panel--mobile {
box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
}
/* Desktop special styles */
.apk-control-panel--desktop {
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
}
5. Combine with Media Queries
When you need more fine-grained control, combine with CSS media queries:
@media (max-width: 768px) {
.my-custom-layout {
flex-direction: column;
}
}
@media (min-width: 769px) and (max-width: 1024px) {
.my-custom-layout {
padding: 20px;
}
}
@media (min-width: 1025px) {
.my-custom-layout {
max-width: 1200px;
margin: 0 auto;
}
}
Related
- PilotKit - Core container component
- Control Components - Control panel responsive details
- Message Components - Message list responsive details
- Style Customization - Custom style guide