|
| 1 | +# @ui5/webcomponents-react-base |
| 2 | + |
| 3 | +Core utilities, hooks, and infrastructure for UI5 Web Components React wrappers. This package is primarily used internally by `@ui5/webcomponents-react` but exports useful hooks and utilities for consumers. |
| 4 | + |
| 5 | +## Import Pattern |
| 6 | + |
| 7 | +Subpath imports are recommended for clarity: |
| 8 | + |
| 9 | +```tsx |
| 10 | +// ✅ Recommended - subpath imports |
| 11 | +import { useI18nBundle, useViewportRange } from '@ui5/webcomponents-react-base/hooks'; |
| 12 | +import * as Device from '@ui5/webcomponents-react-base/Device'; |
| 13 | +import { ThemingParameters } from '@ui5/webcomponents-react-base/ThemingParameters'; |
| 14 | + |
| 15 | +// Also works - root import |
| 16 | +import { useI18nBundle, useViewportRange } from '@ui5/webcomponents-react-base'; |
| 17 | +``` |
| 18 | + |
| 19 | +## Public API |
| 20 | + |
| 21 | +### Hooks (`@ui5/webcomponents-react-base/hooks`) |
| 22 | + |
| 23 | +#### `useI18nBundle(bundleName: string)` |
| 24 | + |
| 25 | +Access internationalization bundles for translated text. |
| 26 | + |
| 27 | +```tsx |
| 28 | +import { useI18nBundle } from '@ui5/webcomponents-react-base/hooks'; |
| 29 | + |
| 30 | +function MyComponent() { |
| 31 | + const i18n = useI18nBundle('@ui5/webcomponents-react'); |
| 32 | + const label = i18n.getText('BUTTON_LABEL'); |
| 33 | + // With placeholders: i18n.getText('ITEMS_SELECTED', 5, 20) -> "5 of 20 items selected" |
| 34 | + |
| 35 | + return <span>{label}</span>; |
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +#### `useViewportRange()` |
| 40 | + |
| 41 | +Get current viewport size range for responsive layouts. |
| 42 | + |
| 43 | +```tsx |
| 44 | +import { useViewportRange } from '@ui5/webcomponents-react-base/hooks'; |
| 45 | + |
| 46 | +function ResponsiveComponent() { |
| 47 | + const range = useViewportRange(); |
| 48 | + // Returns: 'Phone' | 'Tablet' | 'Desktop' | 'LargeDesktop' |
| 49 | + |
| 50 | + return range === 'Phone' ? <MobileView /> : <DesktopView />; |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +### Device Utilities (`@ui5/webcomponents-react-base/Device`) |
| 55 | + |
| 56 | +Re-exports from `@ui5/webcomponents-base/dist/Device.js` plus additional resize/orientation handlers. |
| 57 | + |
| 58 | +```tsx |
| 59 | +import * as Device from '@ui5/webcomponents-react-base/Device'; |
| 60 | + |
| 61 | +// From @ui5/webcomponents-base - check capabilities |
| 62 | +Device.supportsTouch(); |
| 63 | + |
| 64 | +// Resize handling |
| 65 | +Device.attachResizeHandler((windowSize) => { |
| 66 | + console.log(windowSize.width, windowSize.height); |
| 67 | +}); |
| 68 | +Device.detachResizeHandler(handler); |
| 69 | + |
| 70 | +// Orientation handling |
| 71 | +Device.getOrientation(); // { landscape: boolean, portrait: boolean } |
| 72 | +Device.attachOrientationChangeHandler((orientation) => { |
| 73 | + console.log(orientation.landscape, orientation.portrait); |
| 74 | +}); |
| 75 | +Device.detachOrientationChangeHandler(handler); |
| 76 | + |
| 77 | +// Media range queries |
| 78 | +Device.getCurrentRange(); // Current viewport range |
| 79 | +Device.attachMediaHandler(handler); |
| 80 | +Device.detachMediaHandler(handler); |
| 81 | +``` |
| 82 | + |
| 83 | +### ThemingParameters (`@ui5/webcomponents-react-base/ThemingParameters`) |
| 84 | + |
| 85 | +TypeScript-friendly mappings for SAP theming CSS variables. Useful for **inline styles** or **CSS-in-JS** solutions. |
| 86 | + |
| 87 | +```tsx |
| 88 | +import { ThemingParameters } from '@ui5/webcomponents-react-base/ThemingParameters'; |
| 89 | + |
| 90 | +// CSS-in-JS / inline styles |
| 91 | +const style = { |
| 92 | + backgroundColor: ThemingParameters.sapBackgroundColor, // 'var(--sapBackgroundColor)' |
| 93 | + color: ThemingParameters.sapTextColor, // 'var(--sapTextColor)' |
| 94 | + borderRadius: ThemingParameters.sapElement_BorderCornerRadius, |
| 95 | + fontFamily: ThemingParameters.sapFontFamily, |
| 96 | +}; |
| 97 | + |
| 98 | +// Semantic colors |
| 99 | +ThemingParameters.sapPositiveColor; // success/positive |
| 100 | +ThemingParameters.sapNegativeColor; // error/negative |
| 101 | +ThemingParameters.sapCriticalColor; // warning |
| 102 | +ThemingParameters.sapInformativeColor; // info |
| 103 | +``` |
| 104 | + |
| 105 | +**For standard CSS files**, use the CSS variables directly (e.g., `var(--sapBackgroundColor)`). |
| 106 | + |
| 107 | +See the full list of available CSS variables: https://experience.sap.com/fiori-design-web/theming-base-content/ |
| 108 | + |
| 109 | +### Types (`@ui5/webcomponents-react-base/types`) |
| 110 | + |
| 111 | +```tsx |
| 112 | +import type { UI5WCSlotsNode } from '@ui5/webcomponents-react-base/types'; |
| 113 | + |
| 114 | +// UI5WCSlotsNode - Type for slot content (ReactNode | ReactNode[]) |
| 115 | +``` |
| 116 | + |
| 117 | +## Internal APIs |
| 118 | + |
| 119 | +The following exports exist for sharing between packages of this library. **They are not intended for consumer use** and may change without notice: |
| 120 | + |
| 121 | +- `./internal/hooks` - Internal hooks (`useSyncRef`, `useStylesheet`, `useIsRTL`, `useCurrentTheme`, `useIsomorphicLayoutEffect`) |
| 122 | +- `./internal/utils` - Internal utilities (`debounce`, `throttle`, `enrichEventWithDetails`) |
| 123 | +- `./internal/types` - Internal types (`CommonProps`, `Ui5CustomEvent`, `Ui5DomRef`) |
| 124 | +- `./withWebComponent` - HOC for wrapping web components |
0 commit comments