UNPKG

2.54 kBPlain TextView Raw
1'use strict';
2import { PropsAllowlists } from './propsAllowlists';
3import { jsiConfigureProps } from './reanimated2/core';
4function assertNoOverlapInLists() {
5 for (const key in PropsAllowlists.NATIVE_THREAD_PROPS_WHITELIST) {
6 if (key in PropsAllowlists.UI_THREAD_PROPS_WHITELIST) {
7 throw new Error(
8 `[Reanimated] Property \`${key}\` was whitelisted both as UI and native prop. Please remove it from one of the lists.`
9 );
10 }
11 }
12}
13
14export function configureProps(): void {
15 assertNoOverlapInLists();
16 jsiConfigureProps(
17 Object.keys(PropsAllowlists.UI_THREAD_PROPS_WHITELIST),
18 Object.keys(PropsAllowlists.NATIVE_THREAD_PROPS_WHITELIST)
19 );
20}
21
22export function addWhitelistedNativeProps(
23 props: Record<string, boolean>
24): void {
25 const oldSize = Object.keys(
26 PropsAllowlists.NATIVE_THREAD_PROPS_WHITELIST
27 ).length;
28 PropsAllowlists.NATIVE_THREAD_PROPS_WHITELIST = {
29 ...PropsAllowlists.NATIVE_THREAD_PROPS_WHITELIST,
30 ...props,
31 };
32 if (
33 oldSize !==
34 Object.keys(PropsAllowlists.NATIVE_THREAD_PROPS_WHITELIST).length
35 ) {
36 configureProps();
37 }
38}
39
40export function addWhitelistedUIProps(props: Record<string, boolean>): void {
41 const oldSize = Object.keys(PropsAllowlists.UI_THREAD_PROPS_WHITELIST).length;
42 PropsAllowlists.UI_THREAD_PROPS_WHITELIST = {
43 ...PropsAllowlists.UI_THREAD_PROPS_WHITELIST,
44 ...props,
45 };
46 if (
47 oldSize !== Object.keys(PropsAllowlists.UI_THREAD_PROPS_WHITELIST).length
48 ) {
49 configureProps();
50 }
51}
52
53const PROCESSED_VIEW_NAMES = new Set();
54
55export interface ViewConfig {
56 uiViewClassName: string;
57 validAttributes: Record<string, unknown>;
58}
59/**
60 * updates UI props whitelist for given view host instance
61 * this will work just once for every view name
62 */
63
64export function adaptViewConfig(viewConfig: ViewConfig): void {
65 const viewName = viewConfig.uiViewClassName;
66 const props = viewConfig.validAttributes;
67
68 // update whitelist of UI props for this view name only once
69 if (!PROCESSED_VIEW_NAMES.has(viewName)) {
70 const propsToAdd: Record<string, boolean> = {};
71 Object.keys(props).forEach((key) => {
72 // we don't want to add native props as they affect layout
73 // we also skip props which repeat here
74 if (
75 !(key in PropsAllowlists.NATIVE_THREAD_PROPS_WHITELIST) &&
76 !(key in PropsAllowlists.UI_THREAD_PROPS_WHITELIST)
77 ) {
78 propsToAdd[key] = true;
79 }
80 });
81 addWhitelistedUIProps(propsToAdd);
82
83 PROCESSED_VIEW_NAMES.add(viewName);
84 }
85}
86
87configureProps();