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

Table style customizations #337

Closed
techpet opened this issue Jan 15, 2022 · 5 comments
Closed

Table style customizations #337

techpet opened this issue Jan 15, 2022 · 5 comments

Comments

@techpet
Copy link

techpet commented Jan 15, 2022

What is the best way to change the styles of the table internal elements? For example change the thead background-color or remove the border from td?

I've tried overriding them with:

<style type="scss">
table {
	td {
		border:none;
	}
}
</style>

and

<style type="scss">
.my-table {
	td {
		border:none;
	}
}
</style>

<Table class="my-table" ... />

but neither works.

@techpet techpet changed the title Table customizations Table style customizations Jan 15, 2022
@illright
Copy link
Owner

illright commented Jan 15, 2022

Due to the way Svelte scopes its styles, you have to specifically opt out of scoping for your styles to reach the components of Attractions. Try the following:

<style lang="scss">
-.my-table {
+.my-table :global {
	td {
		border:none;
	}
}
</style>

<Table class="my-table" ... />

@techpet
Copy link
Author

techpet commented Jan 17, 2022

Unfortunately that doesn't work either in my component...

Full code here:

<script lang="ts">
	import {Table} from 'attractions';


	const headers = [
    { text: 'First Name', value: 'firstName' },
    { text: 'Last Name', value: 'lastName' },
    { text: 'Age', value: 'age', align: 'end' },
  ];
  const items = [
    { firstName: 'John', lastName: 'Doe', age: 694 },
    { firstName: 'Leo', lastName: 'Tolstoy', age: new Date().getFullYear() - 1828 },
    { firstName: 'فلان', lastName: 'الفلاني', age: 42 },
    { firstName: 'Иван', lastName: 'Иванов', age: 69 },
  ];

</script>

<style lang="scss">
		.my-table :global {
				td {
					border:none;
				}
				
		}
		

</style>

<Table {headers} {items} alternatingRows={false} class="my-table" />

and the result:
image

@techpet
Copy link
Author

techpet commented Jan 17, 2022

If I place the style code inside the global.scss instead of inline into my component it works though. Any ideas?

@illright
Copy link
Owner

illright commented Jan 18, 2022

Oh, wait, sorry, I misled you there a bit. The :global modifier should be placed before .my-table, not after. However, to preserve scoping, you would need to add some scoped selector in the beginning, for example:

<script lang="ts">
  import { Table } from 'attractions';

  const headers = [
    { text: 'First Name', value: 'firstName' },
    { text: 'Last Name', value: 'lastName' },
    { text: 'Age', value: 'age', align: 'end' },
  ];
  const items = [
    { firstName: 'John', lastName: 'Doe', age: 694 },
    { firstName: 'Leo', lastName: 'Tolstoy', age: new Date().getFullYear() - 1828 },
    { firstName: 'فلان', lastName: 'الفلاني', age: 42 },
    { firstName: 'Иван', lastName: 'Иванов', age: 69 },
  ];

</script>

<style lang="scss">
  .something-local :global .my-table {
    td {
      border: none !important;
    }
  }
</style>

<div class="something-local">
  <Table {headers} {items} alternatingRows={false} class="my-table" />
</div>

Note the usage of !important. That's another problem to overcome – specificity. If you have a more specific selector, you can avoid !important, but if not, that should stay.

@techpet
Copy link
Author

techpet commented Jan 18, 2022

Perfect! It works this way, thank you very much!

@techpet techpet closed this as completed Jan 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants