UNPKG

@amaui/style-react

Version:

Amaui CSS in JS styling solution for React

53 lines (48 loc) 1.45 kB
import React from 'react'; import hash from '@amaui/utils/hash'; import merge from '@amaui/utils/merge'; import { inline as amauiInlineMethod } from '@amaui/style'; import { useAmauiStyle, useAmauiTheme } from '.'; export default function inline(value_, props) { let options_ = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { response: 'json' }; const [value, setValue] = React.useState(undefined); const amauiStyle = useAmauiStyle(); const amauiTheme = useAmauiTheme(); const update = function () { let update_ = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; const options = merge(options_, { amaui_style: { value: amauiStyle }, amaui_theme: { value: amauiTheme } }, { copy: true }); // Options response value options.response = 'json'; const valueNew = amauiInlineMethod(value_, props, options); if (update_) setValue(valueNew); return valueNew; }; // Update React.useEffect(() => { update(); if (amauiTheme) amauiTheme.subscriptions.update.subscribe(update); // Clean up return () => { // Unsubscribe if (amauiTheme) amauiTheme.subscriptions.update.unsubscribe(update); }; }, []); // Update props React.useEffect(() => { update(); }, [hash(props)]); // Important for ssr value const value__ = value || update(false); return value__; }