Write the code for this React component:
function VisuallyHidden( {
text,
as: Tag = 'span',
noTranslate = false,
additionalAttributes = {},
} )
It returns an element
text: The innerText of the element
Tag: the html tag of the element
noTranslate: determines if the element has the attribute data-notranslate="true"
additionalAttributes: All other attributes passed into the element
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Below is the React component code that matches the specification you provided. This component, named VisuallyHidden, dynamically renders an HTML element with the specified text, tag type, attributes for translation control, and any additional attributes passed to it.
jsx
import React from 'react';
/**
* VisuallyHidden Component
*
* Renders an element that is hidden from view but remains accessible to screen readers.
*
* @param {Object} props
* @param {string} props.text - The innerText of the element.
* @param {string|React.ElementType} [props.as='span'] - The HTML tag of the element.
* @param {boolean} [props.noTranslate=false] - Determines if the element has the attribute `data-notranslate="true"`.
* @param {Object} [props.additionalAttributes={}] - All other attributes to be passed to the element.
* @param {Object} [props.style] - Custom styles to override default visually hidden styles.
*/
function VisuallyHidden({
text,
as: Tag = 'span',
noTranslate = false,
additionalAttributes = {},
style, // Custom styles to override default
}) {
// Validate the Tag to ensure it's a valid HTML element or React component
if (typeof Tag !== 'string' && typeof Tag !== 'function') {
console.error('Invalid "as" prop. It must be a valid HTML tag or a React component.');
Tag = 'span'; // Default to 'span' if invalid
}
const visuallyHiddenStyle = {
position: 'absolute',
width: '1px',
height: '1px',
padding: '0',
margin: '-1px',
overflow: 'hidden',
clip: 'rect(0, 0, 0, 0)',
whiteSpace: 'nowrap',
borderWidth: '0',
};
const attributes = {
...additionalAttributes,
// Conditionally add the data-notranslate attribute
...(noTranslate && { 'data-notranslate': 'true' }),
// Merge default visually hidden styles with custom styles
style: { ...visuallyHiddenStyle, ...additionalAttributes.style, ...style },
};
return <Tag {...attributes}>{text}</Tag>;
}
export default VisuallyHidden;
Example Use Cases:
Basic Usage
jsx
import React from 'react';
import VisuallyHidden from './VisuallyHidden';
function App() {
return (
<div>
<VisuallyHidden text="This text is visually hidden" />
</div>
);
}
Changing the HTML Tag
jsx
<VisuallyHidden as="div" text="Using a div tag instead of span" />
Disabling Translation
jsx
<VisuallyHidden noTranslate text="This text should not be translated" />
Adding Additional Attributes
jsx
<VisuallyHidden
text="Custom class and aria attribute"
additionalAttributes={{
className: 'custom-class',
'aria-label': 'Label for screen readers',
}}
/>
Overriding Specific Styles
jsx
<VisuallyHidden
text="Override specific style"
style={{
position: 'relative', // Override position style
}}
/>
Using a Custom Component as the Tag
jsx
import MyCustomComponent from './MyCustomComponent';
<VisuallyHidden as={MyCustomComponent} text="Using a custom component" />
Invalid Tag Handling
jsx
<VisuallyHidden as={null} text="Invalid tag will default to span" />