Skip to content

Commit

Permalink
Fix Page Designer ImageWithText Link component (#1092) (#1148)
Browse files Browse the repository at this point in the history
* Fix PD ImageWithText Link component by using Chakra UI Link

* Use isAbsoluteURL to use react-router Link or Chakra Link component

* PR Feedback

* Clean up

* Update packages/template-retail-react-app/app/page-designer/assets/image-with-text/index.jsx



* Remove temporal page-viewer page to facilitate review

---------

Co-authored-by: Ben Chypak <[email protected]>
  • Loading branch information
adamraya and bendvc authored Apr 24, 2023
1 parent 2f73309 commit 733fe86
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*/
import React from 'react'
import PropTypes from 'prop-types'
import {Box, Image, Text} from '@chakra-ui/react'
import {Box, Image, Link as ChakraLink, Text} from '@chakra-ui/react'
import Link from '../../../components/link'
import {isAbsoluteURL} from '../../utils'

/**
* Image with text component
Expand All @@ -22,6 +23,9 @@ import Link from '../../../components/link'
*/
export const ImageWithText = ({ITCLink, ITCText, image, heading, alt}) => {
const hasCaption = ITCText || heading
const isAbsolute = isAbsoluteURL(ITCLink)
const LinkWrapper = isAbsolute ? ChakraLink : Link
const linkProps = isAbsolute ? {href: ITCLink} : {to: ITCLink}

return (
<Box className={'image-with-text'}>
Expand All @@ -35,7 +39,7 @@ export const ImageWithText = ({ITCLink, ITCText, image, heading, alt}) => {
<picture>
<source srcSet={image?.src?.tablet} media="(min-width: 48em)" />
<source srcSet={image?.src?.desktop} media="(min-width: 64em)" />
<Link to={ITCLink}>
<LinkWrapper {...linkProps}>
<Image
className={'image-with-text-image'}
data-testid={'image-with-text-image'}
Expand All @@ -45,7 +49,7 @@ export const ImageWithText = ({ITCLink, ITCText, image, heading, alt}) => {
title={alt}
filter={'brightness(40%)'}
/>
</Link>
</LinkWrapper>
</picture>
{hasCaption && (
<Text as="figcaption">
Expand Down
14 changes: 14 additions & 0 deletions packages/template-retail-react-app/app/page-designer/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* Copyright (c) 2023, Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

/**
* Determines whether the specified URL is absolute
*
* @param {string} url The URL to test
* @returns {boolean} True if the specified URL is absolute, otherwise false
*/
export const isAbsoluteURL = (url) => /^([a-z][a-z\d+\-.]*:)?\/\//i.test(url)

0 comments on commit 733fe86

Please sign in to comment.