Simplify consumption with a generalized StyleProvider#89
Conversation
|
|
||
| // Extend the Window interface to include styleNonce | ||
| // TODO - is this really the way to access a global with TypeScript?? | ||
| declare global { |
There was a problem hiding this comment.
This is what Copilot advised me to do, but I'm suspicious
There was a problem hiding this comment.
Het @rolint, I think that should not work. If I understood this docu correct. It looks like the the window object has no styleNonce attribute. Where does the idea come from that the window object is providing the a styleNonce attribute?
There was a problem hiding this comment.
This comes from a discussion we had (internally) about how to give a consuming application the option to set a global nonce value specifically for styles.
I do not know what the correct technique is for TypeScript to be happy with a global that we invented and is therefore not part of the official definition of the global window object.
There was a problem hiding this comment.
Hey @rolint, I think this following option would be better.
- Create a file
custom.window.ts - Add those content
export interface CustomWindow extends Window { customAttribute: string; } - Now you can import the
CustomWindowin the StyleProvider like that
import { CustomWindow } from './custom.window.ts'; - Than please cast the global window object with CustomWindow
declare let window: CustomWindow; - And now you can use the
customAttributein your code.
window.customAttribute = 'works'; // SET
const value = window.customAttribute; // GET
|
|
||
| const StyleProvider: React.FC<StyleProviderProps> = (props: StyleProviderProps) => { | ||
| // look for <html dir="rtl"> | ||
| const htmlPageDirection = document.documentElement.getAttribute('dir') === 'rtl' ? ThemeDirectionType.RTL : ThemeDirectionType.LTR; |
There was a problem hiding this comment.
Is it considered an antipattern for a React component to reach outside to the document?
There was a problem hiding this comment.
Hey @rolint, no I think not. That should be okay.
|
|
||
| const StyleProvider: React.FC<StyleProviderProps> = (props: StyleProviderProps) => { | ||
| // look for <html dir="rtl"> | ||
| const htmlPageDirection = document.documentElement.getAttribute('dir') === 'rtl' ? ThemeDirectionType.RTL : ThemeDirectionType.LTR; |
There was a problem hiding this comment.
Hey @rolint, no I think not. That should be okay.
|
|
||
| // Extend the Window interface to include styleNonce | ||
| // TODO - is this really the way to access a global with TypeScript?? | ||
| declare global { |
There was a problem hiding this comment.
Het @rolint, I think that should not work. If I understood this docu correct. It looks like the the window object has no styleNonce attribute. Where does the idea come from that the window object is providing the a styleNonce attribute?
work in progress
The expected outcome is an abstraction of the mechanics of styling an app (or an isolated component) with "Enchanted", with a generalized
<StyleProvider>.The StyleProvider also has the following "smarts":
dirattribute of the<html>element, and behave accordinglystyleNoncevariable, for StrictCSPFor example:
Preamble:
Simplest:
Most complex: