-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support ability for consumers to add custom props / HTML attributes defined via configuration to components #723
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,41 @@ | ||
// NOTE: This file is used by the example app. frontend-build expects the file | ||
/* eslint-disable no-console */ | ||
|
||
// NOTE: This file is used by the example app. frontend-build expects the file | ||
// to be in the root of the repository. This is not used by the actual frontend-platform library. | ||
// Also note that in an actual application this file would be added to .gitignore. | ||
// Also note that in an actual application, this file would be added to .gitignore. | ||
const config = { | ||
JS_FILE_VAR: 'JS_FILE_VAR_VALUE_FOR_EXAMPLE_APP', | ||
componentPropOverrides: { | ||
targets: { | ||
example: { | ||
'data-dd-privacy': 'mask', // Custom `data-*` attribute (e.g., Datadog) | ||
'data-hj-suppress': '', // Custom `data-*` attribute (e.g., Hotjar) | ||
className: 'fs-mask', // Custom `className` attribute (e.g., Fullstory) | ||
onClick: (e) => { // Custom `onClick` attribute | ||
console.log('[env.config] onClick event for example', e); | ||
}, | ||
style: { // Custom `style` attribute | ||
background: 'blue', | ||
color: 'white', | ||
}, | ||
}, | ||
example2: { | ||
'data-dd-privacy': 'mask', // Custom `data-*` attribute (e.g., Datadog) | ||
'data-hj-suppress': '', // Custom `data-*` attribute (e.g., Hotjar) | ||
className: 'fs-mask', // Custom `className` attribute (e.g., Fullstory) | ||
onClick: (e) => { // Custom `onClick` attribute | ||
console.log('[env.config] onClick event for example2', e); | ||
}, | ||
style: { // Custom `style` attribute | ||
background: 'blue', | ||
color: 'white', | ||
}, | ||
}, | ||
example3: { | ||
'data-dd-action-name': 'example name', // Custom `data-*` attribute (e.g., Datadog) | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
import { | ||
forwardRef, useContext, useEffect, useRef, useState, | ||
} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { AppContext, useComponentPropOverrides, withComponentPropOverrides } from '@edx/frontend-platform/react'; | ||
|
||
// Example via `useComponentPropOverrides` (hook) | ||
const ExampleComponentWithDefaultPropOverrides = forwardRef(({ children, ...rest }, ref) => { | ||
const propOverrides = useComponentPropOverrides('example', rest); | ||
return <span ref={ref} {...propOverrides}>{children}</span>; | ||
}); | ||
ExampleComponentWithDefaultPropOverrides.displayName = 'ExampleComponentWithDefaultPropOverrides'; | ||
ExampleComponentWithDefaultPropOverrides.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
}; | ||
|
||
const ExampleComponentWithAllowedPropOverrides = forwardRef(({ children, ...rest }, ref) => { | ||
const propOverrides = useComponentPropOverrides('example2', rest, { | ||
allowedPropNames: ['className', 'style', 'onClick'], | ||
}); | ||
return <span ref={ref} {...propOverrides}>{children}</span>; | ||
}); | ||
ExampleComponentWithAllowedPropOverrides.displayName = 'ExampleComponentWithAllowedPropOverrides'; | ||
ExampleComponentWithAllowedPropOverrides.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
}; | ||
|
||
// Example via `withComponentPropOverrides` (HOC) | ||
const ExampleComponent = forwardRef(({ children, ...rest }, ref) => ( | ||
<span ref={ref} {...rest}>{children}</span> | ||
)); | ||
ExampleComponent.displayName = 'ExampleComponent'; | ||
ExampleComponent.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
}; | ||
const ExampleComponentWithPropOverrides3 = withComponentPropOverrides('example3')(ExampleComponent); | ||
|
||
function jsonStringify(obj) { | ||
const replacer = (key, value) => { | ||
if (typeof value === 'function') { | ||
return '[Function]'; | ||
} | ||
return value; | ||
}; | ||
return JSON.stringify(obj, replacer, 2); | ||
} | ||
|
||
function useExample() { | ||
const ref = useRef(null); | ||
const [node, setNode] = useState(null); | ||
|
||
useEffect(() => { | ||
if (ref.current) { | ||
setNode(ref.current.outerHTML); | ||
} | ||
}, []); | ||
|
||
return { | ||
ref, | ||
node, | ||
}; | ||
} | ||
|
||
export default function ComponentPropOverridesPage() { | ||
const { config } = useContext(AppContext); | ||
const firstExample = useExample(); | ||
const secondExample = useExample(); | ||
const thirdExample = useExample(); | ||
|
||
const { componentPropOverrides } = config; | ||
|
||
return ( | ||
<div> | ||
<h1>Example usage of <code>componentPropOverrides</code> from configuration</h1> | ||
|
||
<h2>Current configuration</h2> | ||
{componentPropOverrides ? ( | ||
<pre> | ||
{jsonStringify({ componentPropOverrides })} | ||
</pre> | ||
) : ( | ||
<p> | ||
No <code>componentPropOverrides</code> configuration found. Consider updating this | ||
application's <code>env.config.js</code> to configure any custom props. | ||
</p> | ||
)} | ||
|
||
<h2>Examples</h2> | ||
<p> | ||
The following examples below demonstrate | ||
using <code>useComponentPropOverrides</code> and <code>withComponentPropOverrides</code> to | ||
extend any component's base props based on the application's configuration. Inspect the DOM | ||
elements for the rendered example components below to observe the configured attributes/values. | ||
</p> | ||
|
||
{/* Example 1 (useComponentPropOverrides) */} | ||
<h3><code>useComponentPropOverrides</code> (hook)</h3> | ||
<h4>Default support prop overrides</h4> | ||
<p> | ||
By default, only <code>data-*</code> attributes and <code>className</code> props are | ||
supported; other props will be ignored. You may opt-in to non-default prop | ||
overrides by extending the <code>allowedPropNames</code> option. | ||
</p> | ||
<p> | ||
<ExampleComponentWithDefaultPropOverrides | ||
ref={firstExample.ref} | ||
// eslint-disable-next-line no-console | ||
onClick={(e) => console.log('ExampleComponentWithPropOverrides clicked', e)} | ||
style={{ borderBottom: '4px solid red' }} | ||
className="example-class" | ||
> | ||
Example 1 | ||
</ExampleComponentWithDefaultPropOverrides> | ||
</p> | ||
<i>Result:</i>{' '} | ||
<pre> | ||
<code>{firstExample.node}</code> | ||
</pre> | ||
|
||
{/* Example 2 (useComponentPropOverrides) */} | ||
<h4>Opt-in to specific prop overrides with <code>allowedPropNames</code></h4> | ||
<p> | ||
<ExampleComponentWithAllowedPropOverrides | ||
ref={secondExample.ref} | ||
// eslint-disable-next-line no-console | ||
onClick={(e) => console.log('ExampleComponentWithPropOverrides clicked', e)} | ||
style={{ borderBottom: '4px solid red' }} | ||
className="example-class" | ||
> | ||
Example 2 | ||
</ExampleComponentWithAllowedPropOverrides> | ||
</p> | ||
<i>Result:</i>{' '} | ||
<pre> | ||
<code>{secondExample.node}</code> | ||
</pre> | ||
|
||
{/* Example 3 (withComponentPropOverrides) */} | ||
<h3><code>withComponentPropOverrides</code> (HOC)</h3> | ||
<p> | ||
<ExampleComponentWithPropOverrides3 ref={thirdExample.ref}> | ||
Example 3 | ||
</ExampleComponentWithPropOverrides3> | ||
</p> | ||
<i>Result:</i>{' '} | ||
<pre> | ||
<code>{thirdExample.node}</code> | ||
</pre> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[inform] generates the
global.html
file which is used in links for custom@typedef
types used within the React module. without this, the generated JSDoc link results in a 404. that said, the globals still do not show in the sidebar navigation.