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

Removing inline styles #308

Merged
merged 24 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1c05927
Setting line-height for large size in class instead of inline style
marekdedic Nov 16, 2023
5a2cf92
Moved size-lg to be global and shared between elements
marekdedic Nov 16, 2023
36786ec
Renamed global size-lg class
marekdedic Nov 16, 2023
0b39494
Setting vertical-align in class instead of inline style
marekdedic Nov 16, 2023
e2179eb
Setting transform-origin in class instead of inline style
marekdedic Nov 16, 2023
06a6c56
Setting origin in class instead of inline style
marekdedic Nov 16, 2023
37d7abe
Added class for fixed-width
marekdedic Nov 16, 2023
6360433
fw styles in a class
marekdedic Nov 16, 2023
7c11810
Setting height in class instead of inline style
marekdedic Nov 16, 2023
80d85ed
Setting pull in class instead of inline style
marekdedic Nov 16, 2023
41da178
Setting icon sizes in class instead of inline style
marekdedic Nov 16, 2023
44328da
Replaced static inline styles with a class
marekdedic Nov 16, 2023
c78684a
Setting custom sizes with direct JS element manipulation
marekdedic Nov 16, 2023
7cff0fa
Fixed use of undefined bound element in custom size setting
marekdedic Nov 22, 2023
a65663c
Setting text color with direct JS element manipulation
marekdedic Nov 22, 2023
5ec3506
Setting text transform with direct JS element manipulation
marekdedic Nov 22, 2023
fd59136
Removed obsolete joinCss function
marekdedic Nov 22, 2023
acbdb08
Extracting styles in docs
marekdedic Nov 26, 2023
ed2c8fe
docs build artifact sync
marekdedic Nov 26, 2023
9d896a9
Not including style attribute without styles to not trigger CSP
marekdedic Nov 26, 2023
c54a43e
Updated tests to use computed styles
marekdedic Nov 26, 2023
d5ae23b
Made toggled classes not global
marekdedic Nov 26, 2023
d253597
Fixed test typo
marekdedic Nov 26, 2023
25866fb
Removed docs htaccess, shouldn't have been commited in the first place
marekdedic Nov 26, 2023
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
3 changes: 3 additions & 0 deletions docs/dist/index.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 3 additions & 33 deletions docs/dist/index.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="dist/index.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/combine/npm/bootstrap@4/dist/css/bootstrap.min.css,npm/prismjs@1/themes/prism-okaidia.min.css">
<title>svelte-fa - Tiny FontAwesome 5 component for Svelte</title>
</head>
Expand Down
4 changes: 2 additions & 2 deletions docs/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ export default {
external: ['svelte-fa'],
plugins: [
svelte({
emitCss: false,
emitCss: true,
compilerOptions: {
legacy: true,
css: true,
},
}),
resolve({
Expand All @@ -33,6 +32,7 @@ export default {
extensions: ['.js', '.jsx', '.es6', '.es', '.mjs', '.svelte'],
}),
postcss({
extract: true,
extensions: ['.css'],
}),
],
Expand Down
56 changes: 39 additions & 17 deletions src/fa-layers-text.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<script>
import { onMount } from 'svelte';

Check warning on line 2 in src/fa-layers-text.svelte

View workflow job for this annotation

GitHub Actions / test

'svelte' should be listed in the project's dependencies, not devDependencies

Check warning on line 2 in src/fa-layers-text.svelte

View workflow job for this annotation

GitHub Actions / test

'svelte' should be listed in the project's dependencies, not devDependencies

import {
joinCss,
getStyles,
getTransform,
setCustomSize,
} from './utils';

let clazz = '';
Expand All @@ -19,24 +20,45 @@
export let rotate = '';
export let flip = false;

let s;

$: s = getStyles(
joinCss([
joinCss({
color,
display: 'inline-block',
height: 'auto',
transform: getTransform(scale, translateX, translateY, rotate, flip, undefined, 'em', 'deg'),
}),
style,
]),
size,
);
let containerElement;

onMount(() => {
containerElement.style.color = color;
containerElement.style.transform = getTransform(scale, translateX, translateY, rotate, flip, undefined, 'em', 'deg');
setCustomSize(containerElement, size);
});
</script>

<style>
.container {
display: inline-block;
height: auto;
}

.svelte-fa-size-lg {
font-size: 1.33333em;
line-height: .75em;
vertical-align: -.225em;
}

.svelte-fa-size-sm {
font-size: .875em;
}

.svelte-fa-size-xs {
font-size: .75em;
}
</style>

<span id={id} class="svelte-fa-layers-text {clazz}">
<span style={s}>
<span
bind:this={containerElement}
class="svelte-fa-base container"
class:svelte-fa-size-lg={size === 'lg'}
class:svelte-fa-size-sm={size === 'sm'}
class:svelte-fa-size-xs={size === 'xs'}
style={style !== '' ? style : null}
>
<slot></slot>
</span>
</span>
40 changes: 35 additions & 5 deletions src/fa-layers.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<script>
import { onMount } from 'svelte';

Check warning on line 2 in src/fa-layers.svelte

View workflow job for this annotation

GitHub Actions / test

'svelte' should be listed in the project's dependencies, not devDependencies

Check warning on line 2 in src/fa-layers.svelte

View workflow job for this annotation

GitHub Actions / test

'svelte' should be listed in the project's dependencies, not devDependencies

import {
getStyles,
setCustomSize,
} from './utils';

let clazz = '';
Expand All @@ -11,9 +13,9 @@
export let size = '';
export let pull = '';

let s;
let containerElement;

$: s = getStyles(style, size, pull, true);
onMount(() => { setCustomSize(containerElement, size); });
</script>

<style>
Expand Down Expand Up @@ -42,12 +44,40 @@
.svelte-fa-layers :global(.svelte-fa-layers-text span) {
display: inline-block;
}

.svelte-fa-pull-left {
float: left;
}

.svelte-fa-pull-right {
float: right;
}

.svelte-fa-size-lg {
font-size: 1.33333em;
line-height: .75em;
vertical-align: -.225em;
}

.svelte-fa-size-sm {
font-size: .875em;
}

.svelte-fa-size-xs {
font-size: .75em;
}
</style>

<span
id={id}
class="svelte-fa-layers {clazz}"
style={s}
bind:this={containerElement}
class="svelte-fa-layers svelte-fa-base svelte-fa-fw {clazz}"
class:svelte-fa-pull-left={pull === 'left'}
class:svelte-fa-pull-right={pull === 'right'}
class:svelte-fa-size-lg={size === 'lg'}
class:svelte-fa-size-sm={size === 'sm'}
class:svelte-fa-size-xs={size === 'xs'}
style={style !== '' ? style : null}
>
<slot></slot>
</span>
55 changes: 49 additions & 6 deletions src/fa.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<script>
import { onMount } from 'svelte';

Check warning on line 2 in src/fa.svelte

View workflow job for this annotation

GitHub Actions / test

'svelte' should be listed in the project's dependencies, not devDependencies

Check warning on line 2 in src/fa.svelte

View workflow job for this annotation

GitHub Actions / test

'svelte' should be listed in the project's dependencies, not devDependencies

import {
getStyles,
getTransform,
setCustomSize,
} from './utils';

let clazz = '';
Expand Down Expand Up @@ -34,17 +36,51 @@
export let swapOpacity = false;

let i;
let s;
let transform;
let svgElement;

$: i = (icon && icon.icon) || [0, 0, '', [], ''];
onMount(() => { setCustomSize(svgElement, size); });

$: s = getStyles(style, size, pull, fw);
$: i = (icon && icon.icon) || [0, 0, '', [], ''];

$: transform = getTransform(scale, translateX, translateY, rotate, flip, 512);
</script>

<style>
:global(.svelte-fa-base) {
height: 1em;
overflow: visible;
transform-origin: center;
vertical-align: -.125em;
}

:global(.svelte-fa-fw) {
text-align: center;
width: 1.25em;
}

.svelte-fa-pull-left {
float: left;
}

.svelte-fa-pull-right {
float: right;
}

.svelte-fa-size-lg {
font-size: 1.33333em;
line-height: .75em;
vertical-align: -.225em;
}

.svelte-fa-size-sm {
font-size: .875em;
}

.svelte-fa-size-xs {
font-size: .75em;
}

.spin {
animation: spin 2s 0s infinite linear;
}
Expand All @@ -66,10 +102,17 @@
{#if i[4]}
<svg
id={id || undefined}
class="svelte-fa {clazz}"
bind:this={svgElement}
class="svelte-fa svelte-fa-base {clazz}"
class:pulse
class:svelte-fa-size-lg={size === 'lg'}
class:svelte-fa-size-sm={size === 'sm'}
class:svelte-fa-size-xs={size === 'xs'}
class:svelte-fa-fw={fw}
class:svelte-fa-pull-left={pull === 'left'}
class:svelte-fa-pull-right={pull === 'right'}
class:spin
style={s}
style={style !== '' ? style : null}
viewBox="0 0 {i[0]} {i[1]}"
aria-hidden="true"
role="img"
Expand Down
77 changes: 8 additions & 69 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,9 @@
const parseNumber = parseFloat;

export function joinCss(obj, separator = ';') {
let texts;
if (Array.isArray(obj)) {
texts = obj.filter((text) => text);
} else {
texts = [];
for (const prop in obj) {
if (obj[prop]) {
texts.push(`${prop}:${obj[prop]}`);
}
}
export function setCustomSize(element, size) {
if (size && size !== 'lg' && size !== 'sm' && size !== 'xs') {
element.style.fontSize = size.replace('x', 'em');
}
return texts.join(separator);
}

export function getStyles(style, size, pull, fw) {
let float;
let width;
const height = '1em';
let lineHeight;
let fontSize;
let textAlign;
let verticalAlign = '-.125em';
const overflow = 'visible';

if (fw) {
textAlign = 'center';
width = '1.25em';
}

if (pull) {
float = pull;
}

if (size) {
if (size == 'lg') {
fontSize = '1.33333em';
lineHeight = '.75em';
verticalAlign = '-.225em';
} else if (size == 'xs') {
fontSize = '.75em';
} else if (size == 'sm') {
fontSize = '.875em';
} else {
fontSize = size.replace('x', 'em');
}
}

return joinCss([
joinCss({
float,
width,
height,
'line-height': lineHeight,
'font-size': fontSize,
'text-align': textAlign,
'vertical-align': verticalAlign,
'transform-origin': 'center',
overflow,
}),
style,
]);
}

export function getTransform(
Expand All @@ -87,12 +29,9 @@ export function getTransform(
}
}

return joinCss(
[
`translate(${parseNumber(translateX) * translateTimes}${translateUnit},${parseNumber(translateY) * translateTimes}${translateUnit})`,
`scale(${flipX * parseNumber(scale)},${flipY * parseNumber(scale)})`,
rotate && `rotate(${rotate}${rotateUnit})`,
],
' ',
);
let output = `translate(${parseNumber(translateX) * translateTimes}${translateUnit},${parseNumber(translateY) * translateTimes}${translateUnit}) scale(${flipX * parseNumber(scale)},${flipY * parseNumber(scale)})`;
if (rotate) {
output += ` rotate(${rotate}${rotateUnit})`;
}
return output;
}
Loading
Loading