-
Notifications
You must be signed in to change notification settings - Fork 262
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
Add Rich Text Component #2144
Add Rich Text Component #2144
Conversation
Oxygen deployed a preview of your
Learn more about Hydrogen's GitHub integration. |
} | ||
|
||
const Component = | ||
components[node.type === 'list-item' ? 'listItem' : node.type] ?? |
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.
Follow the conventions of React to switch to camelCase. This makes it so that customizing the list item looks like:
import {RichText} from '@shopify/hydrogen';
export function MainRichText({text}: {text: string}) {
return (
<RichText
data={JSON.parse(text)}
components={{
listItem({node}) {
return <li className="customClass">{node.children}</li>;
},
}}
/>
);
}
switch (node.type) { | ||
case 'root': | ||
return createElement( | ||
Component as Exclude<CustomComponents['root'], undefined>, |
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.
Not sure if there's a way to do this without casting 🤔
components={{ | ||
link: ({node}) => ( | ||
<Link | ||
to={node.url} |
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.
Remix automatically handles if the destination is an external site.
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.
If you want to set up cart metafield to test, here are the steps
fragment CartApiQuery on Cart {
updatedAt
id
+ metafields(identifiers: [{
+ namespace: "custom",
+ key: "richtext",
+ }]){
+ namespace
+ key
+ type
+ value
+ }
checkoutUrl
export function CartMain({layout, cart: originalCart}: CartMainProps) {
const cart = useOptimisticCart(originalCart);
const linesCount = Boolean(cart?.lines?.nodes?.length || 0);
const withDiscount =
cart &&
Boolean(cart?.discountCodes?.filter((code) => code.applicable)?.length);
const className = `cart-main ${withDiscount ? 'with-discount' : ''}`;
+ // @ts-ignore
+ const metafieldValue = (cart.metafields && cart.metafields[0].value) || '';
return (
<div className={className}>
+ <RichText data={metafieldValue} />
mutation cartMetafieldsSet(
$metafields: [CartMetafieldsSetInput!]!
$language: LanguageCode,
$country: CountryCode
) @inContext(
country: $country,
language: $language
) {
cartMetafieldsSet(metafields: $metafields) {
userErrors {
code
elementIndex
field
message
}
}
} and the following variable {
"metafields": {
"ownerId": "gid://shopify/Cart/Z2NwLXVzLWNlbnRyYWwxOjAxSFlYRkZCQ1FCUkVROFFIWDlYM0dOUkRa",
"key": "custom.richtext",
"type": "rich_text_field",
"value": "{\"type\":\"root\",\"children\":[{\"type\":\"heading\",\"level\":2,\"children\":[{\"type\":\"text\",\"value\":\"Heading - cart metafield\"}]},{\"type\":\"paragraph\",\"children\":[{\"type\":\"text\",\"value\":\"This\",\"italic\":true},{\"type\":\"text\",\"value\":\" is a \"},{\"type\":\"text\",\"value\":\"text\",\"bold\":true},{\"type\":\"text\",\"value\":\" and a \"},{\"url\":\"/products/women-crewneck\",\"title\":\"title\",\"type\":\"link\",\"children\":[{\"type\":\"text\",\"value\":\"link\"}]},{\"type\":\"text\",\"value\":\" and an \"},{\"url\":\"https://shopify.com\",\"title\":\"Title\",\"target\":\"_blank\",\"type\":\"link\",\"children\":[{\"type\":\"text\",\"value\":\"external link\"}]}]}]}"
},
"country": "US",
"language": "EN"
} You should see the rich text metafield in cart |
@wizardlyhel thank you for finding the key issue. It should be fixed. |
I'm still seeing the dup key warnings .. maybe the |
A simpler way to test it is with a metafield on the product. Update the Product fragment to include: const PRODUCT_FRAGMENT = `#graphql
fragment Product on Product {
id
title
vendor
handle
descriptionHtml
description
+ extendedDetails:metafield(namespace: "custom", key: "extendeddetails") {
+ type
+ value
+ } And then rendering on the PDP: {product.extendedDetails?.value ? (
<RichText data={product.extendedDetails?.value} />
) : null} |
Just tested this now @blittle - so so good, feels like magic! The only issue I noticed is new lines don't translate to BRs - should they? 🤔 |
WHY are these changes introduced?
Add a RichText component to easily render `rich_text_field` metafields. Thank you @bastienrobert for the original implementation. Fixes #621
Example usage:
See docs at: https://shopify-dev.shopify-dev-8p8h.bret-little.us.spin.dev/docs/api/hydrogen/2024-04/components/richtext
Checklist