Skip to content
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

IconButtonとLikeButtonクラスの定義付け #54

Merged
merged 4 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions packages/tailwindcss/iconButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,9 @@ module.exports = plugin.withOptions(
width: theme("width.8"),
height: theme("height.8"),
position: "relative",
".icon-button-inner": {
width: "100%",
height: "100%",
borderRadius: theme("borderRadius.full"),
display: "inline-flex",
justifyContent: "center",
alignItems: "center",
},
"&:focus": {
outline: "none",
},
"&:active": {
backgroundColor: theme("colors.gray.200"),
},
"&:hover": {
backgroundColor: theme("colors.gray.100"),
color: theme("colors.gray.800"),
Expand All @@ -51,6 +40,9 @@ module.exports = plugin.withOptions(
lineHeight: 1.2,
},
},
"&:active": {
backgroundColor: theme("colors.gray.200"),
},
},
},
]);
Expand Down
59 changes: 59 additions & 0 deletions packages/tailwindcss/lib/iconButtonStyle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const svgToDataUri = require("mini-svg-data-uri");

module.exports = (theme) => {
const heart = (fill, stroke) => {
return {
content: '""',
left: 0,
top: 0,
display: "block",
"background-image": `url("${svgToDataUri(
`<svg width="22" height="19" viewBox="0 0 22 19" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M19.0664 1.96094C16.918 0.164062 13.7539 0.4375 11.7617 2.50781L11.0195 3.28906L10.2383 2.50781C8.63672 0.867188 5.43359 -0.148438 2.93359 1.96094C0.472656 4.07031 0.355469 7.82031 2.54297 10.0859L10.1211 17.8984C10.3555 18.1328 10.668 18.25 10.9805 18.25C11.332 18.25 11.6445 18.1328 11.8789 17.8984L19.457 10.0859C21.6445 7.82031 21.5273 4.07031 19.0664 1.96094ZM18.5586 9.22656L11.0195 17.0391L3.44141 9.22656C1.95703 7.66406 1.64453 4.73438 3.75391 2.9375C5.90234 1.10156 8.40234 2.42969 9.33984 3.36719L11.0195 5.08594L12.6602 3.36719C13.5586 2.42969 16.0977 1.10156 18.2461 2.9375C20.3555 4.73438 20.043 7.66406 18.5586 9.22656Z" fill="${fill}" stroke="${stroke}" /></svg>`
)}")`,
"background-position": `center`,
"background-repeat": `no-repeat`,
"background-size": `${theme("spacing.4")}`,
height: "100%",
};
};
return {
heart: (fill, stroke) => {
return heart(fill, stroke);
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

特定のプロパティをfunctionにするという書き方はすっきりしていていいですね 👍

base: {
Hidetaro7 marked this conversation as resolved.
Show resolved Hide resolved
width: `${theme("spacing.8")}`,
height: `${theme("spacing.8")}`,
display: "inline-flex",
justifyContent: "center",
alignItems: "center",
backgroundColor: theme("colors.white"),
borderRadius: theme("borderRadius.full"),
border: `1px solid currentColor`,
outline: "none",
overflow: "hidden",
cursor: "pointer",
position: "relative",
color: theme("colors.primary.500"),
"&:focus": {
outline: "none",
},
"&:hover": {
backgroundColor: theme("colors.primary.50"),
},
"&:active": {
backgroundColor: theme("colors.primary.100"),
},
},
liked: {
color: theme("colors.white"),
backgroundColor: theme("colors.primary.500"),
border: `1px solid ${theme("colors.primary.500")}`,
"&:hover": {
backgroundColor: theme("colors.primary.600"),
},
"&:active": {
backgroundColor: theme("colors.primary.700"),
},
},
};
};
96 changes: 31 additions & 65 deletions packages/tailwindcss/likeButton.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,48 @@
const plugin = require("tailwindcss/plugin");
const theme = require("./theme");
const svgToDataUri = require("mini-svg-data-uri");
const iconButtonStyle = require("./lib/iconButtonStyle");

module.exports = plugin.withOptions(
() => {
return ({ addComponents, theme }) => {
const { base, heart, liked } = iconButtonStyle(theme);
addComponents([
{
".like-button": {
width: `${theme("spacing.8")}`,
height: `${theme("spacing.8")}`,
display: "inline-flex",
justifyContent: "center",
alignItems: "center",
backgroundColor: theme("colors.white"),
borderRadius: theme("borderRadius.full"),
border: `1px solid ${theme("colors.primary.300")}`,
outline: "none",
overflow: "hidden",
cursor: "pointer",
".icon-button-inner": {
"background-image": `url("${svgToDataUri(
`<svg width="22" height="19" viewBox="0 0 22 19" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M19.0664 1.96094C16.918 0.164062 13.7539 0.4375 11.7617 2.50781L11.0195 3.28906L10.2383 2.50781C8.63672 0.867188 5.43359 -0.148438 2.93359 1.96094C0.472656 4.07031 0.355469 7.82031 2.54297 10.0859L10.1211 17.8984C10.3555 18.1328 10.668 18.25 10.9805 18.25C11.332 18.25 11.6445 18.1328 11.8789 17.8984L19.457 10.0859C21.6445 7.82031 21.5273 4.07031 19.0664 1.96094ZM18.5586 9.22656L11.0195 17.0391L3.44141 9.22656C1.95703 7.66406 1.64453 4.73438 3.75391 2.9375C5.90234 1.10156 8.40234 2.42969 9.33984 3.36719L11.0195 5.08594L12.6602 3.36719C13.5586 2.42969 16.0977 1.10156 18.2461 2.9375C20.3555 4.73438 20.043 7.66406 18.5586 9.22656Z" fill="${theme(
"colors.primary.300"
)}" /></svg>`
)}")`,
"background-position": `center`,
"background-repeat": `no-repeat`,
"background-size": `${theme("spacing.4")}`,
...base,
"&::before": {
...heart("transparent", theme("colors.primary.500")),
width: "100%",
height: "100%",
position: "absolute",
},
"&:focus": {
outline: "none",
},
"&:hover": {
backgroundColor: theme("colors.primary.50"),
},
"&:active": {
backgroundColor: theme("colors.primary.100"),
},
"&.like-button-liked": {
backgroundColor: theme("colors.primary.500"),
border: `1px solid ${theme("colors.primary.500")}`,
".icon-button-inner": {
"background-image": `url("${svgToDataUri(
`<svg width="21" height="19" viewBox="0 0 21 19" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M19.0469 1.96094C16.8984 0.125 13.7344 0.476562 11.7422 2.50781L11 3.28906L10.2188 2.50781C8.26562 0.476562 5.0625 0.125 2.91406 1.96094C0.453125 4.07031 0.335938 7.82031 2.52344 10.0859L10.1016 17.8984C10.5703 18.4062 11.3906 18.4062 11.8594 17.8984L19.4375 10.0859C21.625 7.82031 21.5078 4.07031 19.0469 1.96094Z" fill="${theme(
"colors.white"
)}"/>
</svg>
`
)}")`,
},
".icon-button-label": {
color: theme("colors.white"),
},
"background-position": `center`,
"background-repeat": `no-repeat`,
"background-size": `${theme("spacing.4")}`,
"&:hover": {
backgroundColor: theme("colors.primary.600"),
},
"&:active": {
backgroundColor: theme("colors.primary.700"),
"&[aria-pressed=true]": {
...liked,
"&::before": {
...heart("transparent", theme("colors.white")),
},
},
"&.like-button-labeled": {
width: "auto",
whiteSpace: "nowrap",
color: theme("colors.primary.500"),
".icon-button-inner": {
width: `${theme("spacing.8")}`,
},
".icon-button-label": {
paddingRight: theme("spacing.2"),
fontSize: theme("fontSize.xs"),
},
".labeled-like-button": {
Hidetaro7 marked this conversation as resolved.
Show resolved Hide resolved
...base,
width: "auto",
whiteSpace: "nowrap",
fontSize: theme("fontSize.sm"),
fontWeight: "bold",
padding: `0 ${theme("spacing.2")}`,
lineHeight: 1,
"&::before": {
...heart("transparent", theme("colors.primary.500")),
width: "20px",
position: "relative",
marginRight: theme("spacing.1"),
},
"&[aria-pressed=true]": {
...liked,
"&::before": {
...heart("transparent", theme("colors.white")),
},
/* color: theme("colors.white"),
backgroundColor: theme("colors.primary.500"), */
},
},
},
Expand Down
88 changes: 32 additions & 56 deletions packages/tailwindcss/stories/IconButton.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,82 +4,58 @@ export default {

const Template = () => {
return `
<div class="p-6">
<button type="button" class="icon-button" aria-label="Twitter" data-label="Twitter">
<div class="icon-button-inner">
<div class="p-6">
<button class="icon-button" aria-label="Twitter" data-label="Twitter">
<i class="fab fa-twitter"></i>
</div>
</button>
<button type="button" class="icon-button" aria-label="閉じる" data-label="閉じる">
<div class="icon-button-inner">
</button>
<button class="icon-button" aria-label="閉じる" data-label="閉じる">
<i class="fas fa-times"></i>
</div>
</button>
<button type="button" class="icon-button" aria-label="ホーム" data-label="ホーム">
<div class="icon-button-inner">
</button>
<button class="icon-button" aria-label="ホーム" data-label="ホーム">
<i class="fas fa-home"></i>
</div>
</button>
<button type="button" class="icon-button" aria-label="その他" data-label="その他">
<div class="icon-button-inner">
</button>
<button class="icon-button" aria-label="その他" data-label="その他">
<i class="fas fa-ellipsis-h"></i>
</div>
</button>
</div>
</button>
</div>
`;
};
export const Default = Template.bind({});

const LargeTemplate = () => {
return `
<div class="p-6">
<button type="button" class="icon-button text-lg w-12 h-12" aria-label="Twitter" data-label="Twitter">
<div class="icon-button-inner">
<i class="fab fa-twitter"></i>
</div>
</button>
<button type="button" class="icon-button text-lg w-12 h-12" aria-label="閉じる" data-label="閉じる">
<div class="icon-button-inner">
<i class="fas fa-times"></i>
</div>
</button>
<button type="button" class="icon-button text-lg w-12 h-12" aria-label="ホーム" data-label="ホーム">
<div class="icon-button-inner">
<i class="fas fa-home"></i>
</div>
</button>
<button type="button" class="icon-button text-lg w-12 h-12" aria-label="その他" data-label="その他">
<div class="icon-button-inner">
<i class="fas fa-ellipsis-h"></i>
</div>
</button>
</div>
<div class="p-6">
<button class="icon-button text-lg w-12 h-12" aria-label="Twitter" data-label="Twitter">
<i class="fab fa-twitter"></i>
</button>
<button class="icon-button text-lg w-12 h-12" aria-label="閉じる" data-label="閉じる">
<i class="fas fa-times"></i>
</button>
<button class="icon-button text-lg w-12 h-12" aria-label="ホーム" data-label="ホーム">
<i class="fas fa-home"></i>
</button>
<button class="icon-button text-lg w-12 h-12" aria-label="その他" data-label="その他">
<i class="fas fa-ellipsis-h"></i>
</button>
</div>
`;
};
export const Large = LargeTemplate.bind({});

const SmallTemplate = () => {
return `
<div class="p-6">
<button type="button" class="icon-button text-xs w-6 h-6" aria-label="Twitter" data-label="Twitter">
<div class="icon-button-inner">
<i class="fab fa-twitter"></i>
</div>
<button class="icon-button text-xs w-6 h-6" aria-label="Twitter" data-label="Twitter">
<i class="fab fa-twitter"></i>
</button>
<button type="button" class="icon-button text-xs w-6 h-6" aria-label="閉じる" data-label="閉じる">
<div class="icon-button-inner">
<i class="fas fa-times"></i>
</div>
<button class="icon-button text-xs w-6 h-6" aria-label="閉じる" data-label="閉じる">
<i class="fas fa-times"></i>
</button>
<button type="button" class="icon-button text-xs w-6 h-6" aria-label="ホーム" data-label="ホーム">
<div class="icon-button-inner">
<i class="fas fa-home"></i>
</div>
<button class="icon-button text-xs w-6 h-6" aria-label="ホーム" data-label="ホーム">
<i class="fas fa-home"></i>
</button>
<button type="button" class="icon-button text-xs w-6 h-6" aria-label="その他" data-label="その他">
<div class="icon-button-inner">
<i class="fas fa-ellipsis-h"></i>
</div>
<button class="icon-button text-xs w-6 h-6" aria-label="その他" data-label="その他">
<i class="fas fa-ellipsis-h"></i>
</button>
</div>
`;
Expand Down
17 changes: 17 additions & 0 deletions packages/tailwindcss/stories/LabeledLikeButton.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default {
title: "Buttons/LabeledLikeButton",
};

const Template = () => {
return `
<button class="labeled-like-button">いいね</button>
`;
};
export const Default = Template.bind({});

const LikedLabeledTemplate = () => {
return `
<button class="labeled-like-button" aria-pressed="true">いいね</button>
`;
};
export const LikedLabeled = LikedLabeledTemplate.bind({});
40 changes: 2 additions & 38 deletions packages/tailwindcss/stories/LikeButton.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,14 @@ export default {

const Template = () => {
return `
<div class="p-6">
<a type="button" class="like-button">
<div class="icon-button-inner"></div>
</a>
</div>
<a class="like-button"></a>
`;
};
export const Default = Template.bind({});

const LikedTemplate = () => {
return `
<div class="p-6">
<a type="button" class="like-button like-button-liked">
<div class="icon-button-inner"></div>
</a>
</div>
<a class="like-button" aria-pressed="true"></a>
Hidetaro7 marked this conversation as resolved.
Show resolved Hide resolved
`;
};
export const Liked = LikedTemplate.bind({});

const LabeledTemplate = () => {
return `
<div class="p-6">
<a type="button" class="like-button like-button-labeled">
<div class="icon-button-inner"></div>
<div class="icon-button-label">
いいね
</div>
</a>
</div>
`;
};
export const Labeled = LabeledTemplate.bind({});

const LikedLabeledTemplate = () => {
return `
<div class="p-6">
<a type="button" class="like-button like-button-liked like-button-labeled">
<div class="icon-button-inner"></div>
<div class="icon-button-label">
いいね
</div>
</a>
</div>
`;
};
export const LikedLabeled = LikedLabeledTemplate.bind({});