-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathuse-base-component.ts
More file actions
54 lines (46 loc) · 2.05 KB
/
use-base-component.ts
File metadata and controls
54 lines (46 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { MutableRefObject } from "react";
import {
ComponentConfiguration,
initAwsUiVersions,
useComponentMetadata,
} from "@cloudscape-design/component-toolkit/internal";
import { PACKAGE_SOURCE, PACKAGE_VERSION, THEME } from "../environment";
import { getVisualTheme } from "../utils/get-visual-theme";
import { useTelemetry } from "./use-telemetry";
import { useVisualRefresh } from "./use-visual-refresh";
initAwsUiVersions(PACKAGE_SOURCE, PACKAGE_VERSION);
export interface InternalBaseComponentProps {
__internalRootRef?: MutableRefObject<any> | null;
}
/**
* This hook is used for components which are exported to customers. The returned __internalRootRef needs to be
* attached to the (internal) component's root DOM node. The hook takes care of attaching the metadata to this
* root DOM node and emits the telemetry for this component.
*/
export default function useBaseComponent<T = any>(componentName: string, config?: ComponentConfiguration) {
useTelemetry(componentName, config);
const isVisualRefresh = useVisualRefresh();
const theme = getVisualTheme(THEME, isVisualRefresh);
const elementRef = useComponentMetadata<T>(componentName, {
packageName: PACKAGE_SOURCE,
version: PACKAGE_VERSION,
theme,
});
return { __internalRootRef: elementRef };
}
// we also support data-* attributes, but they are always implicitly allowed by typescript
// http://www.typescriptlang.org/docs/handbook/jsx.html#attribute-type-checking
// "Note: If an attribute name is not a valid JS identifier (like a data-* attribute), it is not considered to be an error"
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface BaseComponentProps {}
export function getBaseProps(props: BaseComponentProps) {
const baseProps: Record<string, string> = {};
Object.keys(props).forEach((prop) => {
if (prop.startsWith("data-")) {
baseProps[prop] = (props as Record<string, string>)[prop];
}
});
return baseProps;
}