This repository was archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 140
ShopPage Component example #220
Open
jeparlefrancais
wants to merge
18
commits into
Roblox:master
Choose a base branch
from
jeparlefrancais:tutorial-shop-page
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ce728bd
Initial example code and tutorial general lines
jeparlefrancais b46e782
Use validateProps and small fixes
jeparlefrancais 37f35a1
Move validateProps to top
jeparlefrancais f0dd2b9
Move validateProps to top of file
jeparlefrancais bb66313
Remove props validation
jeparlefrancais b86ec48
Move props in top of render
jeparlefrancais 07e8593
Add hover animations
jeparlefrancais 8b62a91
Begin tutorial docs
jeparlefrancais 0437194
Improve tutorial content
jeparlefrancais 6e2edfd
Add product item content
jeparlefrancais c526ae5
Improve tutorial
jeparlefrancais 2908bed
Add shop-page to examples demo place
jeparlefrancais f00e5ae
Revise wording so things flow better
ce99d19
Merge pull request #1 from vocksel/revisions
jeparlefrancais 5a833fb
Add tutorial content
jeparlefrancais f889eff
Change cellSize and height default value
jeparlefrancais 5786521
Merge branch 'master' into tutorial-shop-page
jeparlefrancais 0f908e6
Fix site rendering issues and add preview image
jeparlefrancais 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,82 @@ | ||
local MarketplaceService = game:GetService("MarketplaceService") | ||
local Players = game:GetService("Players") | ||
local ReplicatedStorage = game:GetService("ReplicatedStorage") | ||
local TweenService = game:GetService("TweenService") | ||
|
||
local Roact = require(ReplicatedStorage.Roact) | ||
|
||
local Item = Roact.Component:extend("Item") | ||
|
||
local PADDING = 20 | ||
|
||
function Item:init() | ||
self.ref = Roact.createRef() | ||
self.onMouseEnter = function() | ||
self.toBigIcon:Play() | ||
end | ||
self.onMouseLeave = function() | ||
self.toNormalIcon:Play() | ||
end | ||
self.onActivated = function() | ||
local props = self.props | ||
|
||
MarketplaceService:PromptProductPurchase(Players.LocalPlayer, props.productId) | ||
end | ||
end | ||
|
||
function Item:render() | ||
local props = self.props | ||
|
||
local image = props.image | ||
local price = props.price | ||
local order = props.order or price | ||
|
||
return Roact.createElement("ImageButton", { | ||
BackgroundTransparency = 1, | ||
Image = "", | ||
LayoutOrder = order, | ||
[Roact.Event.Activated] = self.onActivated, | ||
[Roact.Event.MouseEnter] = self.onMouseEnter, | ||
[Roact.Event.MouseLeave] = self.onMouseLeave, | ||
}, { | ||
Icon = Roact.createElement("ImageLabel", { | ||
AnchorPoint = Vector2.new(0.5, 0.5), | ||
BackgroundTransparency = 1, | ||
Image = image, | ||
Position = UDim2.new(0.5, 0, 0.5, 0), | ||
Size = UDim2.new(1, -PADDING, 1, -PADDING), | ||
[Roact.Ref] = self.ref, | ||
}), | ||
PriceLabel = Roact.createElement("TextLabel", { | ||
AnchorPoint = Vector2.new(0.5, 1), | ||
BackgroundTransparency = 1, | ||
Font = Enum.Font.SourceSans, | ||
Text = ("R$ %d"):format(price), | ||
TextColor3 = Color3.fromRGB(10, 200, 10), | ||
TextScaled = true, | ||
TextStrokeTransparency = 0, | ||
TextStrokeColor3 = Color3.fromRGB(255, 255, 255), | ||
Position = UDim2.new(0.5, 0, 1, 0), | ||
Size = UDim2.new(1, 0, 0.3, 0), | ||
}), | ||
}) | ||
end | ||
|
||
function Item:didMount() | ||
local tweenInfo = TweenInfo.new(0.2) | ||
local icon = self.ref:getValue() | ||
|
||
self.toBigIcon = TweenService:Create(icon, tweenInfo, { | ||
Size = UDim2.new(1, 0, 1, 0), | ||
}) | ||
self.toNormalIcon = TweenService:Create(icon, tweenInfo, { | ||
Size = UDim2.new(1, -PADDING, 1, -PADDING), | ||
}) | ||
end | ||
|
||
function Item:willUnmount() | ||
self.toBigIcon:Destroy() | ||
self.toNormalIcon:Destroy() | ||
end | ||
|
||
return Item |
This file contains hidden or 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,28 @@ | ||
local ReplicatedStorage = game:GetService("ReplicatedStorage") | ||
|
||
local Components = script.Parent | ||
|
||
local Roact = require(ReplicatedStorage.Roact) | ||
|
||
local Item = require(Components:WaitForChild("Item")) | ||
|
||
local function ItemList(props) | ||
local items = props.items | ||
|
||
local elements = {} | ||
|
||
for i=1, #items do | ||
local item = items[i] | ||
|
||
elements[item.identifier] = Roact.createElement(Item, { | ||
image = item.image, | ||
price = item.price, | ||
productId = item.productId, | ||
order = item.order, | ||
}) | ||
end | ||
|
||
return Roact.createFragment(elements) | ||
end | ||
|
||
return ItemList |
This file contains hidden or 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,73 @@ | ||
local ReplicatedStorage = game:GetService("ReplicatedStorage") | ||
|
||
local Components = script.Parent | ||
|
||
local Roact = require(ReplicatedStorage.Roact) | ||
local ItemList = require(Components:WaitForChild("ItemList")) | ||
|
||
local ShopPage = Roact.Component:extend("ShopPage") | ||
|
||
ShopPage.defaultProps = { | ||
padding = 0, | ||
} | ||
|
||
function ShopPage:init() | ||
self:setState({ | ||
cellSize = 0, | ||
canvasHeight = 0, | ||
}) | ||
|
||
self.onAbsoluteSizeChanged = function(frame) | ||
local props = self.props | ||
local padding = props.padding | ||
local itemsPerRow = props.itemsPerRow | ||
|
||
local totalWidth = frame.AbsoluteSize.X | ||
local cellWidth = (totalWidth - padding * (itemsPerRow + 1)) / itemsPerRow | ||
local cellHeight = cellWidth / props.itemAspectRatio | ||
local rows = math.ceil(#props.items / itemsPerRow) | ||
local canvasHeight = rows * cellHeight + padding * (rows + 1) | ||
|
||
self:setState({ | ||
cellSize = cellWidth, | ||
canvasHeight = canvasHeight, | ||
}) | ||
end | ||
end | ||
|
||
function ShopPage:render() | ||
local props = self.props | ||
local state = self.state | ||
|
||
local items = props.items | ||
local itemAspectRatio = props.itemAspectRatio | ||
local frameProps = props.frameProps | ||
local padding = props.padding | ||
|
||
local cellSize = state.cellSize | ||
local canvasHeight = state.canvasHeight | ||
|
||
local newFrameProps = {} | ||
|
||
for key, value in pairs(frameProps) do | ||
newFrameProps[key] = value | ||
end | ||
jeparlefrancais marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
newFrameProps.CanvasSize = UDim2.new(0, 0, 0, canvasHeight) | ||
newFrameProps[Roact.Change.AbsoluteSize] = self.onAbsoluteSizeChanged | ||
|
||
return Roact.createElement("ScrollingFrame", newFrameProps, { | ||
UIGrid = Roact.createElement("UIGridLayout", { | ||
CellPadding = UDim2.new(0, padding, 0, padding), | ||
CellSize = UDim2.new(0, cellSize, 0, cellSize / itemAspectRatio), | ||
HorizontalAlignment = Enum.HorizontalAlignment.Center, | ||
VerticalAlignment = Enum.VerticalAlignment.Center, | ||
SortOrder = Enum.SortOrder.LayoutOrder, | ||
}), | ||
Items = Roact.createElement(ItemList, { | ||
items = items, | ||
}), | ||
}) | ||
end | ||
|
||
return ShopPage |
This file contains hidden or 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,38 @@ | ||
return { | ||
{ | ||
identifier = "Red Visor", | ||
image = "https://www.roblox.com/asset-thumbnail/image?assetId=139618072&width=420&height=420&format=png", | ||
price = 30, | ||
productId = 0, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we use real productIds and use those as the stable keys for the list items so we can get rid of the identifier field? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hum I could put random values... Even if we keep the I think at first my plan was to show the name identifier in a label but I dropped that. |
||
}, | ||
{ | ||
identifier = "Green Visor", | ||
image = "https://www.roblox.com/asset-thumbnail/image?assetId=20264649&width=420&height=420&format=png", | ||
price = 50, | ||
productId = 0, | ||
}, | ||
{ | ||
identifier = "Yellow Visor", | ||
image = "https://www.roblox.com/asset-thumbnail/image?assetId=7135977&width=420&height=420&format=png", | ||
price = 40, | ||
productId = 0, | ||
}, | ||
{ | ||
identifier = "Blue Visor", | ||
image = "https://www.roblox.com/asset-thumbnail/image?assetId=1459035&width=420&height=420&format=png", | ||
price = 55, | ||
productId = 0, | ||
}, | ||
{ | ||
identifier = "Dark Blue Visor", | ||
image = "https://www.roblox.com/asset-thumbnail/image?assetId=68268372&width=420&height=420&format=png", | ||
price = 60, | ||
productId = 0, | ||
}, | ||
{ | ||
identifier = "Purple Visor", | ||
image = "https://www.roblox.com/asset-thumbnail/image?assetId=334661971&width=420&height=420&format=png", | ||
price = 100, | ||
productId = 0, | ||
}, | ||
} |
This file contains hidden or 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,36 @@ | ||
local ReplicatedStorage = game:GetService("ReplicatedStorage") | ||
local Players = game:GetService("Players") | ||
|
||
local Roact = require(ReplicatedStorage.Roact) | ||
|
||
local Products = require(script:WaitForChild("Products")) | ||
|
||
local Components = script:WaitForChild("Components") | ||
local ShopPage = require(Components:WaitForChild("ShopPage")) | ||
|
||
return function() | ||
local screenGui = Instance.new("ScreenGui") | ||
screenGui.Parent = Players.LocalPlayer:WaitForChild("PlayerGui") | ||
|
||
local shopPage = Roact.createElement(ShopPage, { | ||
items = Products, | ||
itemAspectRatio = 1, | ||
itemsPerRow = 3, | ||
padding = 10, | ||
frameProps = { | ||
AnchorPoint = Vector2.new(0.5, 0.5), | ||
BackgroundColor3 = Color3.fromRGB(0, 3, 20), | ||
BorderSizePixel = 0, | ||
Position = UDim2.new(0.5, 0, 0.5, 0), | ||
Size = UDim2.new(0.5, 0, 0.5, 0), | ||
}, | ||
}) | ||
|
||
local handle = Roact.mount(shopPage, screenGui) | ||
|
||
local function stop() | ||
Roact.unmount(handle) | ||
end | ||
|
||
return stop | ||
end |
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.
Why do we need WaitForChild here? Is it unsafe for children of a folder to assume its siblings are available?
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.
I don't think there is any guarantee that all the siblings are replicated. From my experience it's best to use WaitForChild since you're sure everything is there when you need it.
Maybe @LPGhatguy can validate this!