-
Notifications
You must be signed in to change notification settings - Fork 683
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create categoryTile mixin and move makeurl to peregrine
- Loading branch information
Showing
4 changed files
with
90 additions
and
79 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
packages/peregrine/lib/mixins/CategoryList/useCategoryTile.js
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,39 @@ | ||
import { useMemo } from 'react'; | ||
|
||
// TODO: get categoryUrlSuffix from graphql storeOptions when it is ready | ||
const categoryUrlSuffix = '.html'; | ||
const previewImageSize = 480; | ||
|
||
export const useCategoryTile = props => { | ||
const { item, resourceUrl } = props; | ||
const { image, productImagePreview } = item; | ||
|
||
const imagePath = useMemo(() => { | ||
const previewProduct = productImagePreview.items[0]; | ||
if (image) { | ||
return resourceUrl(image, { | ||
type: 'image-category', | ||
width: previewImageSize | ||
}); | ||
} else if (previewProduct) { | ||
return resourceUrl(previewProduct.small_image, { | ||
type: 'image-product', | ||
width: previewImageSize | ||
}); | ||
} else { | ||
return null; | ||
} | ||
}, [image, productImagePreview, resourceUrl]); | ||
|
||
// interpolation doesn't work inside `url()` for legacy reasons | ||
// so a custom property should wrap its value in `url()` | ||
const imageUrl = imagePath ? `url(${imagePath})` : 'none'; | ||
const imageWrapperStyle = { '--venia-image': imageUrl }; | ||
|
||
return { | ||
imagePath, | ||
imageWrapperStyle, | ||
itemName: item.name, | ||
itemUrl: `/${item.url_key}${categoryUrlSuffix}` | ||
}; | ||
}; |
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
126 changes: 49 additions & 77 deletions
126
packages/venia-ui/lib/components/CategoryList/categoryTile.js
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,80 +1,52 @@ | ||
import React, { Component } from 'react'; | ||
import React from 'react'; | ||
import { arrayOf, string, shape } from 'prop-types'; | ||
import classify from '../../classify'; | ||
import { mergeClasses } from '../../classify'; | ||
import { Link, resourceUrl } from '@magento/venia-drivers'; | ||
import defaultClasses from './categoryTile.css'; | ||
|
||
// TODO: get categoryUrlSuffix from graphql storeOptions when it is ready | ||
const categoryUrlSuffix = '.html'; | ||
|
||
const previewImageSize = 480; | ||
|
||
class CategoryTile extends Component { | ||
static propTypes = { | ||
item: shape({ | ||
image: string, | ||
name: string.isRequired, | ||
productImagePreview: shape({ | ||
items: arrayOf( | ||
shape({ | ||
small_image: string | ||
}) | ||
) | ||
}), | ||
url_key: string.isRequired | ||
}).isRequired, | ||
classes: shape({ | ||
item: string, | ||
image: string, | ||
imageWrapper: string, | ||
name: string | ||
}).isRequired | ||
}; | ||
|
||
get imagePath() { | ||
const { image, productImagePreview } = this.props.item; | ||
const previewProduct = productImagePreview.items[0]; | ||
if (image) { | ||
return resourceUrl(image, { | ||
type: 'image-category', | ||
width: previewImageSize | ||
}); | ||
} else if (previewProduct) { | ||
return resourceUrl(previewProduct.small_image, { | ||
type: 'image-product', | ||
width: previewImageSize | ||
}); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
render() { | ||
const { imagePath, props } = this; | ||
const { classes, item } = props; | ||
|
||
// interpolation doesn't work inside `url()` for legacy reasons | ||
// so a custom property should wrap its value in `url()` | ||
const imageUrl = imagePath ? `url(${imagePath})` : 'none'; | ||
const style = { '--venia-image': imageUrl }; | ||
|
||
// render an actual image element for accessibility | ||
const imagePreview = imagePath ? ( | ||
<img className={classes.image} src={imagePath} alt={item.name} /> | ||
) : null; | ||
|
||
return ( | ||
<Link | ||
className={classes.root} | ||
to={`/${item.url_key}${categoryUrlSuffix}`} | ||
> | ||
<span className={classes.imageWrapper} style={style}> | ||
{imagePreview} | ||
</span> | ||
<span className={classes.name}>{item.name}</span> | ||
</Link> | ||
); | ||
} | ||
} | ||
|
||
export default classify(defaultClasses)(CategoryTile); | ||
import { useCategoryTile } from '@magento/peregrine/lib/mixins/CategoryList/useCategoryTile'; | ||
|
||
const CategoryTile = props => { | ||
const mixinProps = useCategoryTile({ | ||
item: props.item, | ||
resourceUrl | ||
}); | ||
|
||
const { imagePath, imageWrapperStyle, itemName, itemUrl } = mixinProps; | ||
const classes = mergeClasses(defaultClasses, props.classes); | ||
|
||
// render an actual image element for accessibility | ||
const imagePreview = imagePath ? ( | ||
<img className={classes.image} src={imagePath} alt={itemName} /> | ||
) : null; | ||
|
||
return ( | ||
<Link className={classes.root} to={itemUrl}> | ||
<span className={classes.imageWrapper} style={imageWrapperStyle}> | ||
{imagePreview} | ||
</span> | ||
<span className={classes.name}>{itemName}</span> | ||
</Link> | ||
); | ||
}; | ||
|
||
CategoryTile.propTypes = { | ||
item: shape({ | ||
image: string, | ||
name: string.isRequired, | ||
productImagePreview: shape({ | ||
items: arrayOf( | ||
shape({ | ||
small_image: string | ||
}) | ||
) | ||
}), | ||
url_key: string.isRequired | ||
}).isRequired, | ||
classes: shape({ | ||
item: string, | ||
image: string, | ||
imageWrapper: string, | ||
name: string | ||
}).isRequired | ||
}; | ||
export default CategoryTile; |
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